def _handle_error_response(self, response: httpclient.HTTPResponse):
        try:
            parsed_body = ET.fromstring(response.body)

            if EbxmlErrorEnvelope.is_ebxml_error(parsed_body):
                _, parsed_response = ebxml_handler.handle_ebxml_error(response.code,
                                                                      response.headers,
                                                                      response.body)
                logger.warning('Received ebxml errors from Spine. {HTTPStatus} {Errors}',
                               fparams={'HTTPStatus': response.code, 'Errors': parsed_response})
            elif SOAPFault.is_soap_fault(parsed_body):
                _, parsed_response, _ = handle_soap_error(response.code,
                                                          response.headers,
                                                          response.body)
                logger.warning('Received soap errors from Spine. {HTTPStatus} {Errors}',
                               fparams={'HTTPStatus': response.code, 'Errors': parsed_response})
            else:
                logger.warning("Received an unexpected response from Spine",
                               fparams={'HTTPStatus': response.code})
                parsed_response = "Didn't get expected response from Spine"

        except ET.ParseError:
            logger.exception('Unable to parse response from Spine.')
            parsed_response = 'Unable to handle response returned from Spine'

        return 500, parsed_response, None
Example #2
0
    def test_single_error(self):
        message = file_utilities.get_file_string(
            self.message_dir / 'ebxml_response_error_single.xml')
        resp_json = json.loads(
            handle_ebxml_error(200, {'Content-Type': 'text/xml'}, message)[1])

        self.assert_json_error_root(resp_json)
        self.assert_json_with_first_error(resp_json)
Example #3
0
    def test_multiple_errors(self):
        message = FileUtilities.get_file_string(
            self.message_dir / 'ebxml_response_error_multiple.xml')
        response = handle_ebxml_error(200, {'Content-Type': 'text/xml'},
                                      message)[1]

        self.assertIn('501319:Unknown eb:CPAId', response)
        self.assertIn('501320:Unknown something else', response)
        self.assertIn('errorType=ebxml_error', response)
Example #4
0
    def _handle_error_response(self, response: httpclient.HTTPResponse,
                               num_of_retries: int,
                               retries_remaining: List[int]):
        try:
            parsed_body = ET.fromstring(response.body)

            if EbxmlErrorEnvelope.is_ebxml_error(parsed_body):
                _, parsed_response = ebxml_handler.handle_ebxml_error(
                    response.code, response.headers, response.body)
                logger.warning(
                    '0007',
                    'Received ebxml errors from Spine. {HTTPStatus} {Errors}',
                    {
                        'HTTPStatus': response.code,
                        'Errors': parsed_response
                    })

            elif SOAPFault.is_soap_fault(parsed_body):
                _, parsed_response, soap_fault_codes = handle_soap_error(
                    response.code, response.headers, response.body)
                logger.warning(
                    '0008',
                    'Received soap errors from Spine. {HTTPStatus} {Errors}', {
                        'HTTPStatus': response.code,
                        'Errors': parsed_response
                    })

                if SOAPFault.is_soap_fault_retriable(soap_fault_codes):
                    logger.warning(
                        "0015",
                        "A retriable error was encountered {error} {retries_remaining} "
                        "{max_retries}", {
                            "error": parsed_response,
                            "retries_remaining": retries_remaining[0],
                            "max_retries": num_of_retries
                        })
                    if retries_remaining[0] <= 0:
                        # exceeded the number of retries so return the SOAP error response
                        logger.error(
                            "0016",
                            "A request has exceeded the maximum number of retries, {max_retries} "
                            "retries", {"max_retries": num_of_retries})
                    else:
                        raise _NeedToRetryException()
            else:
                logger.warning('0017',
                               "Received an unexpected response from Spine",
                               {'HTTPStatus': response.code})
                parsed_response = "Didn't get expected response from Spine"

        except ET.ParseError as pe:
            logger.warning('0010',
                           'Unable to parse response from Spine. {Exception}',
                           {'Exception': repr(pe)})
            parsed_response = 'Unable to handle response returned from Spine'

        return 500, parsed_response, None
Example #5
0
    def test_empty_body(self):
        code, body = handle_ebxml_error(200, {'Content-Type': 'text/xml'}, '')

        self.assertEqual(code, 200)
        self.assertEqual(body, '')
Example #6
0
 def test_non_xml_content_type(self):
     with self.assertRaises(ValueError):
         handle_ebxml_error(200, {'Content-Type': 'text/html'}, 'Some body')
Example #7
0
 def test_no_content_type(self):
     with self.assertRaises(ValueError):
         handle_ebxml_error(200, {}, 'Some body')
Example #8
0
 def test_invalid_xml(self):
     with self.assertRaises(ElementTree.ParseError):
         handle_ebxml_error(200, {'Content-Type': 'text/xml'},
                            '<a><b><b></a>')
Example #9
0
 def test_non_ebxml_fault(self):
     self.assertEqual(
         handle_ebxml_error(200, {'Content-Type': 'text/xml'},
                            '<a><b></b></a>'), (200, '<a><b></b></a>'))
Example #10
0
 def test_non_200(self):
     self.assertEqual(
         handle_ebxml_error(202, {'Content-Type': 'text/xml'}, ''),
         (202, ''))
Example #11
0
 def test_single_error(self):
     message = FileUtilities.get_file_string(
         self.message_dir / 'ebxml_response_error_single.xml')
     self.assertIn(
         '501319:Unknown eb:CPAId',
         handle_ebxml_error(200, {'Content-Type': 'text/xml'}, message)[1])