Skip to content

Commit

Permalink
(fix) SDK error parsing during SDK request abort
Browse files Browse the repository at this point in the history
  • Loading branch information
danh91 committed Jul 15, 2023
1 parent 5d5b903 commit 050f9ca
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 38 deletions.
4 changes: 2 additions & 2 deletions requirements.server.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ django-debug-toolbar
# -e ./sdk/extensions/dicom
-e ./sdk/extensions/dpd
# -e ./sdk/extensions/dpdhl
# -e ./sdk/extensions/fedex
-e ./sdk/extensions/fedex
# -e ./sdk/extensions/geodis
# -e ./sdk/extensions/laposte
# -e ./sdk/extensions/nationex
Expand Down Expand Up @@ -56,7 +56,7 @@ karrio.dhl-universal
karrio.dicom
# karrio.dpd
karrio.dpdhl
karrio.fedex
# karrio.fedex
karrio.geodis
karrio.laposte
karrio.nationex
Expand Down
4 changes: 4 additions & 0 deletions sdk/core/karrio/api/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def abort(
Returns:
Tuple[None, List[models.Message]]: a tuple of empty response and a list or error messages
"""

if isinstance(error, errors.ParsedMessagesError):
return None, error.messages

return (
None,
[
Expand Down
41 changes: 12 additions & 29 deletions sdk/core/karrio/core/errors.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
"""Karrio Custom Errors(Exception) definition modules"""
import warnings
from enum import Enum
from typing import Dict


class DeprecatedClass:
deprecation_message = "This class is deprecated and will be removed soon"

def __init__(self, *args, **kwargs):
warnings.warn(self.deprecation_message, DeprecationWarning)
super().__init__(*args, **kwargs)


class FieldErrorCode(Enum):
required = dict(code="required", message="This field is required")
invalid = dict(code="invalid", message="This field is invalid")
Expand All @@ -27,8 +18,8 @@ class ShippingSDKError(Exception):
class ShippingSDKDetailedError(ShippingSDKError):
"""Base class for other exceptions."""

def __init__(self, *args):
self.details = None
def __init__(self, *args, details=None):
self.details = details
super().__init__(*args)


Expand All @@ -42,6 +33,16 @@ def __init__(self, fields: Dict[str, FieldErrorCode]):
self.details = {name: code.value for name, code in fields.items()}


class ParsedMessagesError(ShippingSDKDetailedError):
"""Raised when one or many required fields are missing."""

code = "SHIPPING_SDK_FIELD_ERROR"

def __init__(self, messages = []):
super().__init__("Invalid request payload")
self.messages = messages


class ValidationError(ShippingSDKError):
"""Base class for other exceptions."""

Expand Down Expand Up @@ -82,21 +83,3 @@ class MultiParcelNotSupportedError(ShippingSDKError):

def __init__(self):
super().__init__(f"Multi-parcel shipment not supported")


"""Deprecated Custom Errors"""


class PurplShipError(Exception, DeprecatedClass):
"""Base class for other exceptions."""

code = "SHIPPING_SDK_INTERNAL_ERROR"
deprecation_message = "PurplShipError is deprecated and will be removed soon. use ShippingSDKError instead"


class PurplShipDetailedError(PurplShipError):
"""Base class for other exceptions."""

def __init__(self, *args):
self.details = None
super().__init__(*args)
7 changes: 4 additions & 3 deletions sdk/extensions/boxknight/karrio/providers/boxknight/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jstruct
import karrio.lib as lib
import karrio.core as core
import karrio.core.errors as errors


class Settings(core.Settings):
Expand Down Expand Up @@ -52,9 +53,9 @@ def authenticate(settings: Settings):
method="POST",
)
response = lib.to_dict(result)
errors = error.parse_error_response(response, settings)
messages = error.parse_error_response(response, settings)

if any(errors):
raise Exception(errors)
if any(messages):
raise errors.ParsedMessagesError(messages=messages)

return dict(token=response["token"])
8 changes: 5 additions & 3 deletions sdk/extensions/dpd/karrio/providers/dpd/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import dpd_lib.Authentication20 as auth
import dpd_lib.LoginServiceV21 as dpd

import jstruct
import datetime
import karrio.lib as lib
import karrio.core as core
import karrio.core.errors as errors


class Settings(core.Settings):
Expand Down Expand Up @@ -108,10 +110,10 @@ def login(settings: Settings):
},
)
response = lib.to_element(result)
errors = error.parse_error_response(response, settings)
messages = error.parse_error_response(response, settings)

if any(errors):
raise Exception(errors)
if any(messages):
raise errors.ParsedMessagesError(messages=messages)

return _extract_login_details(response)

Expand Down
2 changes: 1 addition & 1 deletion sdk/extensions/ups/karrio/providers/ups/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def login(settings: Settings):
messages = error.parse_error_response(response, settings)

if any(messages):
raise errors.ShippingSDKError(messages)
raise errors.ParsedMessagesError(messages=messages)

expiry = datetime.datetime.fromtimestamp(
float(response.get("issued_at")) / 1000
Expand Down

0 comments on commit 050f9ca

Please sign in to comment.