Skip to content

Commit

Permalink
(improve) DPDHL error message and details status
Browse files Browse the repository at this point in the history
  • Loading branch information
danh91 committed Jul 15, 2023
1 parent 1f4d1dc commit 0d28b54
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 36 deletions.
4 changes: 2 additions & 2 deletions requirements.server.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,7 +55,7 @@ karrio.dhl-poland
karrio.dhl-universal
karrio.dicom
# karrio.dpd
karrio.dpdhl
# karrio.dpdhl
# karrio.fedex
karrio.geodis
karrio.laposte
Expand Down
63 changes: 34 additions & 29 deletions sdk/extensions/dpdhl/karrio/providers/dpdhl/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,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=" ",
)
}
Expand All @@ -100,11 +103,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]
Expand Down
43 changes: 38 additions & 5 deletions sdk/extensions/dpdhl/tests/dpdhl/test_shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -152,17 +166,16 @@ 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": "Die angegebene Straße kann nicht gefunden werden. Der "
"eingegebene Wert ist zu lang und wurde gekürzt.",
},
],
]
Expand All @@ -177,6 +190,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,
[
Expand Down

0 comments on commit 0d28b54

Please sign in to comment.