diff --git a/requirements.server.dev.txt b/requirements.server.dev.txt index 9c1875fe00..e83abf91f1 100644 --- a/requirements.server.dev.txt +++ b/requirements.server.dev.txt @@ -18,7 +18,7 @@ django-debug-toolbar # -e ./sdk/extensions/dhl_universal # -e ./sdk/extensions/dicom -e ./sdk/extensions/dpd -# -e ./sdk/extensions/dpdhl +-e ./sdk/extensions/dpdhl -e ./sdk/extensions/fedex # -e ./sdk/extensions/geodis # -e ./sdk/extensions/laposte @@ -55,7 +55,7 @@ karrio.dhl-poland karrio.dhl-universal karrio.dicom # karrio.dpd -karrio.dpdhl +# karrio.dpdhl # karrio.fedex karrio.geodis karrio.laposte diff --git a/sdk/extensions/dpdhl/karrio/providers/dpdhl/error.py b/sdk/extensions/dpdhl/karrio/providers/dpdhl/error.py index e46bd8baae..3fa422f25a 100644 --- a/sdk/extensions/dpdhl/karrio/providers/dpdhl/error.py +++ b/sdk/extensions/dpdhl/karrio/providers/dpdhl/error.py @@ -1,4 +1,3 @@ -from urllib.error import HTTPError import dpdhl_lib.business_interface as dpdhl import typing import karrio.lib as lib @@ -55,36 +54,39 @@ def _parse_xml_error_response( carrier_id=settings.carrier_id, carrier_name=settings.carrier_name, code=error.statusCode, - message=error.statusText, - details={ - **( - lib.failsafe( - lambda: lib.to_dict( - { - "message": lib.join( - *[ - _ - for _ in error.statusMessage - if isinstance(_, str) - ], - join=" ", - ) - } - ) + message=( + lib.failsafe( + lambda: lib.join( + *set([_ for _ in error.statusMessage if isinstance(_, str)]), + join=" ", ) - or {} - if any(error.statusMessage or []) - else {} - ), + ) + or lib.failsafe( + lambda: lib.join( + *set( + [ + f"{_.statusElement}: {_.statusMessage}" + for _ in error.errorMessage + ] + ), + join=" ", + ) + ) + or error.statusText + ), + details={ + "statusText": error.statusText, **( lib.failsafe( lambda: lib.to_dict( { "error": lib.join( - *[ - f"{_.statusElement}: {_.statusMessage}" - for _ in error.errorMessage - ], + *set( + [ + f"{_.statusElement}: {_.statusMessage}" + for _ in error.errorMessage + ] + ), join=" ", ) } @@ -100,11 +102,13 @@ def _parse_xml_error_response( { "warning": lib.join( *( - [ - _ - for _ in error.warningMessage - if isinstance(_, str) - ] + set( + [ + _ + for _ in error.warningMessage + if isinstance(_, str) + ] + ) if isinstance(error.warningMessage, list) else ( [error.warningMessage] diff --git a/sdk/extensions/dpdhl/tests/dpdhl/test_shipment.py b/sdk/extensions/dpdhl/tests/dpdhl/test_shipment.py index 6ec4809e6d..c3c2ab75eb 100644 --- a/sdk/extensions/dpdhl/tests/dpdhl/test_shipment.py +++ b/sdk/extensions/dpdhl/tests/dpdhl/test_shipment.py @@ -69,6 +69,20 @@ def test_parse_cancel_shipment_response(self): lib.to_dict(parsed_response), ParsedCancelShipmentResponse ) + def test_parse_error_response(self): + with patch("karrio.mappers.dpdhl.proxy.lib.request") as mock: + mock.return_value = ShipmentErrorResponse + parsed_response = ( + karrio.Shipment.cancel(self.ShipmentCancelRequest) + .from_(gateway) + .parse() + ) + + self.assertListEqual( + lib.to_dict(parsed_response), + ParsedErrorResponse, + ) + def test_parse_html_error_response(self): with patch("karrio.mappers.dpdhl.proxy.lib.request") as mock: mock.return_value = HTMLErrorResponse @@ -152,17 +166,15 @@ def test_parse_html_error_response(self): "carrier_id": "dpdhl", "carrier_name": "dpdhl", "code": 0, - "details": {}, + "details": {"statusText": "Weak validation error occured."}, "message": "Weak validation error occured.", }, { "carrier_id": "dpdhl", "carrier_name": "dpdhl", "code": 0, - "details": { - "message": "Der eingegebene Wert ist zu lang und wurde gekürzt. Die angegebene Straße kann nicht gefunden werden. Die angegebene Straße kann nicht gefunden werden. Der eingegebene Wert ist zu lang und wurde gekürzt." - }, - "message": "Weak validation error occured.", + "details": {"statusText": "Weak validation error occured."}, + "message": ANY, }, ], ] @@ -177,6 +189,26 @@ def test_parse_html_error_response(self): [], ] +ParsedErrorResponse = [ + None, + [ + { + "carrier_id": "dpdhl", + "carrier_name": "dpdhl", + "code": 1101, + "details": {"statusText": "Hard validation error occured."}, + "message": "Hard validation error occured.", + }, + { + "carrier_id": "dpdhl", + "carrier_name": "dpdhl", + "code": 1101, + "details": {"statusText": "Hard validation error occured."}, + "message": "Bitte geben Sie ein gültiges Sendungsdatum an.", + }, + ], +] + ParsedHTMLErrorResponse = [ None, [