How to use the easypost.errors.ApiError function in easypost

To help you get started, we’ve selected a few easypost examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github EasyPost / easypost-python / easypost / api.py View on Github external
def handle_api_error(cls, response):
        if not (200 <= response.status_code < 300):
            raise ApiError(response)
github EasyPost / easypost-python / easypost / errors.py View on Github external
def __init__(self, response, message=None):
        super(ApiError, self).__init__(message)
        status_code = response.status_code
        try:
            json = response.json()
        except ValueError:
            raise Exception('Status: <{}> JSON response from API could not be decoded.'.format(status_code))  # FIXME: is this useful? should i just let it raise its own exception?

        try:
            error = json['error']  # FIXME: not confident that this is the best way to access errors
            raise Exception(error.get('message', ''))
        except KeyError:
            raise Exception('Invalid response from API: <{}> {}'.format(status_code, json))