def specific_api_error(self, rbody, rcode, resp, rheaders, error_data):
        util.log_info(
            'Stripe API error received',
            error_code=error_data.get('code'),
            error_type=error_data.get('type'),
            error_message=error_data.get('message'),
            error_param=error_data.get('param'),
        )

        # Rate limits were previously coded as 400's with code 'rate_limit'
        if rcode == 429 or (rcode == 400
                            and error_data.get('code') == 'rate_limit'):
            return error.RateLimitError(error_data.get('message'), rbody,
                                        rcode, resp, rheaders)
        elif rcode in [400, 404]:
            return error.InvalidRequestError(error_data.get('message'),
                                             error_data.get('param'), rbody,
                                             rcode, resp, rheaders)
        elif rcode == 401:
            return error.AuthenticationError(error_data.get('message'), rbody,
                                             rcode, resp, rheaders)
        elif rcode == 402:
            return error.CardError(error_data.get('message'),
                                   error_data.get('param'),
                                   error_data.get('code'), rbody, rcode, resp,
                                   rheaders)
        elif rcode == 403:
            return error.PermissionError(error_data.get('message'), rbody,
                                         rcode, resp, rheaders)
        else:
            return error.APIError(error_data.get('message'), rbody, rcode,
                                  resp, rheaders)
Esempio n. 2
0
    def handle_api_error(self, rbody, rcode, resp, rheaders):
        try:
            err = resp['error']
        except (KeyError, TypeError):
            raise error.APIError(
                "Invalid response object from API: %r (HTTP response code "
                "was %d)" % (rbody, rcode),
                rbody, rcode, resp)

        # Rate limits were previously coded as 400's with code 'rate_limit'
        if rcode == 429 or (rcode == 400 and err.get('code') == 'rate_limit'):
            raise error.RateLimitError(
                err.get('message'), rbody, rcode, resp, rheaders)
        elif rcode in [400, 404]:
            raise error.InvalidRequestError(
                err.get('message'), err.get('param'),
                rbody, rcode, resp, rheaders)
        elif rcode == 401:
            raise error.AuthenticationError(
                err.get('message'), rbody, rcode, resp,
                rheaders)
        elif rcode == 402:
            raise error.CardError(err.get('message'), err.get('param'),
                                  err.get('code'), rbody, rcode, resp,
                                  rheaders)
        elif rcode == 403:
            raise error.PermissionError(
                err.get('message'), rbody, rcode, resp,
                rheaders)
        else:
            raise error.APIError(err.get('message'), rbody, rcode, resp,
                                 rheaders)