1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
from __future__ import unicode_literals
from flask_api import status
class APIException(Exception):
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
detail = ''
def __init__(self, detail=None):
if detail is not None:
self.detail = detail
def __str__(self):
return self.detail
class ParseError(APIException):
status_code = status.HTTP_400_BAD_REQUEST
detail = 'Malformed request.'
class AuthenticationFailed(APIException):
status_code = status.HTTP_401_UNAUTHORIZED
detail = 'Incorrect authentication credentials.'
class NotAuthenticated(APIException):
status_code = status.HTTP_401_UNAUTHORIZED
detail = 'Authentication credentials were not provided.'
class PermissionDenied(APIException):
status_code = status.HTTP_403_FORBIDDEN
detail = 'You do not have permission to perform this action.'
class NotFound(APIException):
status_code = status.HTTP_404_NOT_FOUND
detail = 'This resource does not exist.'
# class MethodNotAllowed(APIException):
# status_code = status.HTTP_405_METHOD_NOT_ALLOWED
# detail = 'Request method "%s" not allowed.'
# def __init__(self, method, detail=None):
# self.detail = (detail or self.detail) % method
class NotAcceptable(APIException):
status_code = status.HTTP_406_NOT_ACCEPTABLE
detail = 'Could not satisfy the request Accept header.'
class UnsupportedMediaType(APIException):
status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
detail = 'Unsupported media type in the request Content-Type header.'
class Throttled(APIException):
status_code = status.HTTP_429_TOO_MANY_REQUESTS
detail = 'Request was throttled.'
# def __init__(self, wait=None, detail=None):
# if wait is None:
# self.detail = detail or self.detail
# self.wait = None
# else:
# format = (detail or self.detail) + ' ' + self.extra_detail
# self.detail = format % (wait, wait != 1 and 's' or '')
# self.wait = math.ceil(wait)
|