Skip to content

Commit

Permalink
Refactore error handling (#76)
Browse files Browse the repository at this point in the history
Refactor error handling
  • Loading branch information
hparfr authored and florian-dacosta committed May 31, 2023
1 parent 70cf673 commit 5a89027
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions delivery_roulier/models/stock_package.py
Expand Up @@ -8,12 +8,15 @@

from openerp import models, api
from openerp.tools.translate import _
from openerp.exceptions import Warning as UserError
from openerp.exceptions import UserError

_logger = logging.getLogger(__name__)
try:
from roulier import roulier
from roulier.exception import InvalidApiInput
from roulier.exception import (
InvalidApiInput,
CarrierError
)
except ImportError:
_logger.debug('Cannot `import roulier`.')

Expand Down Expand Up @@ -90,7 +93,11 @@ def _get_parcel(self, picking):
pass

@implemented_by_carrier
def _error_handling(self, payload, response):
def _carrier_error_handling(self, payload, response):
pass

@implemented_by_carrier
def _invalid_api_input_handling(self, payload, response):
pass

@implemented_by_carrier
Expand Down Expand Up @@ -143,13 +150,10 @@ def _call_roulier_api(self, picking):
# api call
ret = roulier_instance.get_label(payload)
except InvalidApiInput as e:
raise UserError(self._error_handling(payload, e.message))
except Exception as e:
raise UserError(e.message)
raise UserError(self._invalid_api_input_handling(payload, e))
except CarrierError as e:
raise UserError(self._carrier_error_handling(payload, e))

# minimum error handling
if ret.get('status', '') == 'error':
raise UserError(self._error_handling(payload, ret))
# give result to someone else
return self._after_call(picking, ret)

Expand Down Expand Up @@ -229,6 +233,9 @@ def _roulier_should_include_customs(self, picking):
return sender.country_id.code != receiver.country_id.code

@api.model
def _roulier_error_handling(self, payload, response):
def _roulier_carrier_error_handling(self, payload, exception):
return _(u'Sent data:\n%s\n\nException raised:\n%s\n' % (
payload, response))
payload, exception.message))

def _roulier_invalid_api_input_handling(self, payload, exception):
return _(u'Bad input: %s\n', exception.message)

0 comments on commit 5a89027

Please sign in to comment.