Return application/problem+json format errors and provide error endpoint
to dereference error codes.
This commit is contained in:
parent
8c81915f38
commit
9c08717173
7 changed files with 156 additions and 39 deletions
|
@ -1,9 +1,10 @@
|
|||
import logging
|
||||
import datetime
|
||||
import json
|
||||
from enum import Enum
|
||||
|
||||
from app import app, metric_queue
|
||||
from flask import Blueprint, request, make_response, jsonify, session
|
||||
from flask import Blueprint, Response, request, make_response, jsonify, session, url_for
|
||||
from flask.ext.restful import Resource, abort, Api, reqparse
|
||||
from flask.ext.restful.utils.cors import crossdomain
|
||||
from calendar import timegm
|
||||
|
@ -33,7 +34,50 @@ api.decorators = [csrf_protect,
|
|||
process_oauth, time_decorator(api_bp.name, metric_queue)]
|
||||
|
||||
|
||||
class ApiErrorType(Enum):
|
||||
external_service_timeout = 'external_service_timeout'
|
||||
invalid_request = 'invalid_request'
|
||||
invalid_response = 'invalid_response'
|
||||
invalid_token = 'invalid_token'
|
||||
insufficient_scope = 'insufficient_scope'
|
||||
fresh_login_required = 'fresh_login_required'
|
||||
exceeds_license = 'exceeds_license'
|
||||
not_found = 'not_found'
|
||||
downstream_issue = 'downstream_issue'
|
||||
|
||||
|
||||
ERROR_DESCRIPTION = {
|
||||
ApiErrorType.external_service_timeout.value: "An external service timed out. Retrying the request may resolve the issue.",
|
||||
ApiErrorType.invalid_request.value: "The request was invalid. It may have contained invalid values or was improperly formatted.",
|
||||
ApiErrorType.invalid_response.value: "The response was invalid.",
|
||||
ApiErrorType.invalid_token.value: "The access token provided was invalid. It may have expired.",
|
||||
ApiErrorType.insufficient_scope.value: "The access token did not have sufficient scope to access the requested resource.",
|
||||
ApiErrorType.fresh_login_required.value: "The action requires a fresh login to succeed.",
|
||||
ApiErrorType.exceeds_license.value: "The action was refused because the current license does not allow it.",
|
||||
ApiErrorType.not_found.value: "The resource was not found.",
|
||||
ApiErrorType.downstream_issue.value: "An error occurred in a downstream service.",
|
||||
}
|
||||
|
||||
|
||||
class ApiException(Exception):
|
||||
"""
|
||||
o "type" (string) - A URI reference that identifies the
|
||||
problem type.
|
||||
|
||||
o "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
|
||||
|
||||
o "status" (number) - The HTTP status code
|
||||
|
||||
o "detail" (string) - A human-readable explanation specific to this
|
||||
occurrence of the problem.
|
||||
|
||||
o "instance" (string) - A URI reference that identifies the specific
|
||||
occurrence of the problem. It may or may not yield further
|
||||
information if dereferenced.
|
||||
"""
|
||||
|
||||
def __init__(self, error_type, status_code, error_description, payload=None):
|
||||
Exception.__init__(self)
|
||||
self.error_description = error_description
|
||||
|
@ -41,76 +85,80 @@ class ApiException(Exception):
|
|||
self.payload = payload
|
||||
self.error_type = error_type
|
||||
|
||||
print(self)
|
||||
|
||||
def to_dict(self):
|
||||
rv = dict(self.payload or ())
|
||||
if self.error_description is not None:
|
||||
rv['error_description'] = self.error_description
|
||||
|
||||
rv['error_type'] = self.error_type
|
||||
if self.error_description is not None:
|
||||
rv['detail'] = self.error_description
|
||||
|
||||
rv['title'] = self.error_type.value
|
||||
rv['type'] = url_for('error', error_type=self.error_type.value, _external=True)
|
||||
rv['status'] = self.status_code
|
||||
|
||||
return rv
|
||||
|
||||
|
||||
class ExternalServiceTimeout(ApiException):
|
||||
def __init__(self, error_description, payload=None):
|
||||
ApiException.__init__(self, 'external_service_timeout', 520, error_description, payload)
|
||||
ApiException.__init__(self, ApiErrorType.external_service_timeout, 520, error_description, payload)
|
||||
|
||||
|
||||
class InvalidRequest(ApiException):
|
||||
def __init__(self, error_description, payload=None):
|
||||
ApiException.__init__(self, 'invalid_request', 400, error_description, payload)
|
||||
ApiException.__init__(self, ApiErrorType.invalid_request, 400, error_description, payload)
|
||||
|
||||
|
||||
class InvalidResponse(ApiException):
|
||||
def __init__(self, error_description, payload=None):
|
||||
ApiException.__init__(self, 'invalid_response', 400, error_description, payload)
|
||||
ApiException.__init__(self, ApiErrorType.invalid_response, 400, error_description, payload)
|
||||
|
||||
|
||||
class InvalidToken(ApiException):
|
||||
def __init__(self, error_description, payload=None):
|
||||
ApiException.__init__(self, 'invalid_token', 401, error_description, payload)
|
||||
ApiException.__init__(self, ApiErrorType.invalid_token, 401, error_description, payload)
|
||||
|
||||
|
||||
class Unauthorized(ApiException):
|
||||
def __init__(self, payload=None):
|
||||
user = get_authenticated_user()
|
||||
if user is None or user.organization:
|
||||
ApiException.__init__(self, 'invalid_token', 401, "Requires authentication", payload)
|
||||
ApiException.__init__(self, ApiErrorType.invalid_token, 401, "Requires authentication", payload)
|
||||
else:
|
||||
ApiException.__init__(self, 'insufficient_scope', 403, 'Unauthorized', payload)
|
||||
ApiException.__init__(self, ApiErrorType.insufficient_scope, 403, 'Unauthorized', payload)
|
||||
|
||||
|
||||
class FreshLoginRequired(ApiException):
|
||||
def __init__(self, payload=None):
|
||||
ApiException.__init__(self, 'fresh_login_required', 401, "Requires fresh login", payload)
|
||||
ApiException.__init__(self, ApiErrorType.fresh_login_required, 401, "Requires fresh login", payload)
|
||||
|
||||
|
||||
class ExceedsLicenseException(ApiException):
|
||||
def __init__(self, payload=None):
|
||||
ApiException.__init__(self, None, 402, 'Payment Required', payload)
|
||||
ApiException.__init__(self, ApiErrorType.exceeds_license, 402, 'Payment Required', payload)
|
||||
|
||||
|
||||
class NotFound(ApiException):
|
||||
def __init__(self, payload=None):
|
||||
ApiException.__init__(self, None, 404, 'Not Found', payload)
|
||||
ApiException.__init__(self, ApiErrorType.not_found, 404, 'Not Found', payload)
|
||||
|
||||
|
||||
class DownstreamIssue(ApiException):
|
||||
def __init__(self, payload=None):
|
||||
ApiException.__init__(self, None, 520, 'Downstream Issue', payload)
|
||||
ApiException.__init__(self, ApiErrorType.downstream_issue, 520, 'Downstream Issue', payload)
|
||||
|
||||
|
||||
@api_bp.app_errorhandler(ApiException)
|
||||
@crossdomain(origin='*', headers=['Authorization', 'Content-Type'])
|
||||
def handle_api_error(error):
|
||||
response = jsonify(error.to_dict())
|
||||
response.status_code = error.status_code
|
||||
if error.error_type is not None:
|
||||
print(error)
|
||||
response = Response(json.dumps(error.to_dict()), error.status_code, mimetype='application/problem+json')
|
||||
if error.status_code is 401:
|
||||
response.headers['WWW-Authenticate'] = ('Bearer error="%s" error_description="%s"' %
|
||||
(error.error_type, error.error_description))
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def resource(*urls, **kwargs):
|
||||
def wrapper(api_resource):
|
||||
if not api_resource:
|
||||
|
@ -383,6 +431,8 @@ def request_error(exception=None, **kwargs):
|
|||
message = 'Request error.'
|
||||
if exception:
|
||||
message = exception.message
|
||||
if 'message' in data.keys():
|
||||
message = data.pop('message')
|
||||
raise InvalidRequest(message, data)
|
||||
|
||||
|
||||
|
@ -427,6 +477,7 @@ def define_json_response(schema_name):
|
|||
import endpoints.api.billing
|
||||
import endpoints.api.build
|
||||
import endpoints.api.discovery
|
||||
import endpoints.api.error
|
||||
import endpoints.api.image
|
||||
import endpoints.api.logs
|
||||
import endpoints.api.organization
|
||||
|
|
56
endpoints/api/error.py
Normal file
56
endpoints/api/error.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
""" Error details API """
|
||||
from flask import url_for
|
||||
from enum import Enum
|
||||
|
||||
from endpoints.api import (resource, nickname, ApiResource, NotFound, path_param,
|
||||
define_json_response, ApiErrorType, ERROR_DESCRIPTION)
|
||||
|
||||
|
||||
def error_view(error_type):
|
||||
return {
|
||||
'type': url_for('error', error_type=error_type, _external=True),
|
||||
'title': error_type,
|
||||
'description': ERROR_DESCRIPTION[error_type]
|
||||
}
|
||||
|
||||
|
||||
@resource('/v1/error/<error_type>')
|
||||
@path_param('error_type', 'The error code identifying the type of error.')
|
||||
class Error(ApiResource):
|
||||
""" Resource for manging an organization's teams. """
|
||||
schemas = {
|
||||
'ApiError': {
|
||||
'type': 'object',
|
||||
'description': 'Description of an error',
|
||||
'required': [
|
||||
'type',
|
||||
'properties',
|
||||
'title',
|
||||
],
|
||||
'properties': {
|
||||
'type': {
|
||||
'type': 'string',
|
||||
'description': 'A reference to the error type resource'
|
||||
},
|
||||
'title': {
|
||||
'type': 'string',
|
||||
'description': 'The title of the error. Can be used to uniquely identify the kind of error.',
|
||||
'enum': list(ApiErrorType.__members__)
|
||||
},
|
||||
'description': {
|
||||
'type': 'string',
|
||||
'description': 'A more detailed description of the error that may include help for fixing the issue.'
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@define_json_response('ApiError')
|
||||
@nickname('getError')
|
||||
def get(self, error_type):
|
||||
""" Get a detailed description of the error """
|
||||
if error_type in ERROR_DESCRIPTION.keys():
|
||||
return error_view(error_type), 200
|
||||
else:
|
||||
raise NotFound()
|
||||
|
Reference in a new issue