2018-05-21 21:02:38 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
from flask import url_for
|
|
|
|
from werkzeug.exceptions import HTTPException
|
|
|
|
|
|
|
|
|
|
|
|
class ApiErrorType(Enum):
|
2018-08-15 19:32:24 +00:00
|
|
|
invalid_request = 'invalid_request'
|
2018-05-21 21:02:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ApiException(HTTPException):
|
2018-08-15 19:32:24 +00:00
|
|
|
"""
|
|
|
|
Represents an error in the application/problem+json format.
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
See: https://tools.ietf.org/html/rfc7807
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
- "type" (string) - A URI reference that identifies the
|
|
|
|
problem type.
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
- "title" (string) - A short, human-readable summary of the problem
|
|
|
|
type. It SHOULD NOT change from occurrence to occurrence of the
|
|
|
|
problem, except for purposes of localization
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
- "status" (number) - The HTTP status code
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
- "detail" (string) - A human-readable explanation specific to this
|
|
|
|
occurrence of the problem.
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
- "instance" (string) - A URI reference that identifies the specific
|
|
|
|
occurrence of the problem. It may or may not yield further
|
|
|
|
information if dereferenced.
|
|
|
|
"""
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def __init__(self, error_type, status_code, error_description, payload=None):
|
|
|
|
Exception.__init__(self)
|
|
|
|
self.error_description = error_description
|
|
|
|
self.code = status_code
|
|
|
|
self.payload = payload
|
|
|
|
self.error_type = error_type
|
|
|
|
self.data = self.to_dict()
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
super(ApiException, self).__init__(error_description, None)
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
def to_dict(self):
|
|
|
|
rv = dict(self.payload or ())
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
if self.error_description is not None:
|
|
|
|
rv['detail'] = self.error_description
|
|
|
|
rv['error_message'] = self.error_description # TODO: deprecate
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
rv['error_type'] = self.error_type.value # TODO: deprecate
|
|
|
|
rv['title'] = self.error_type.value
|
|
|
|
rv['type'] = url_for('api.error', error_type=self.error_type.value, _external=True)
|
|
|
|
rv['status'] = self.code
|
2018-05-21 21:02:38 +00:00
|
|
|
|
2018-08-15 19:32:24 +00:00
|
|
|
return rv
|
2018-05-21 21:02:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidRequest(ApiException):
|
2018-08-15 19:32:24 +00:00
|
|
|
def __init__(self, error_description, payload=None):
|
|
|
|
ApiException.__init__(self, ApiErrorType.invalid_request, 400, error_description, payload)
|
2018-05-21 21:02:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidResponse(ApiException):
|
2018-08-15 19:32:24 +00:00
|
|
|
def __init__(self, error_description, payload=None):
|
|
|
|
ApiException.__init__(self, ApiErrorType.invalid_response, 400, error_description, payload)
|