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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
"""
Errors
~~~~~~
Exceptions that get transformed to HTTP error responses.
:copyright: Copyright 2018 PlanGrid, Inc., see AUTHORS.
:license: MIT, see LICENSE for details.
"""
from flask_rebar.messages import ErrorMessage
from typing import Any, Dict, Optional, Union
class HttpJsonError(Exception):
"""
Abstract base class for exceptions that will be cause and transformed
into an appropriate HTTP error response with a JSON body.
These can be raised at any time during the handling of a request,
and the Rebar extension will handling catching it and transforming it.
This class itself shouldn't be used. Instead, use one of the subclasses.
:param str msg:
A human readable message to be included in the JSON error response
:param dict additional_data:
Dictionary of additional keys and values to be set in the JSON body.
Note that these keys and values are added to the root object of the
response, not nested under "additional_data".
"""
default_message: str
http_status_code: int
def __init__(
self,
msg: Optional[Union[str, ErrorMessage]] = None,
additional_data: Optional[Dict[str, Any]] = None,
) -> None:
self.error_message = msg or self.default_message
self.additional_data = additional_data
super().__init__(self.error_message)
class BadRequest(HttpJsonError):
http_status_code, default_message = 400, "Bad Request"
class Unauthorized(HttpJsonError):
http_status_code, default_message = 401, "Unauthorized"
class PaymentRequired(HttpJsonError):
http_status_code, default_message = 402, "Payment Required"
class Forbidden(HttpJsonError):
http_status_code, default_message = 403, "Forbidden"
class NotFound(HttpJsonError):
http_status_code, default_message = 404, "Not Found"
class MethodNotAllowed(HttpJsonError):
http_status_code, default_message = 405, "Method Not Allowed"
class NotAcceptable(HttpJsonError):
http_status_code, default_message = 406, "Not Acceptable"
class ProxyAuthenticationRequired(HttpJsonError):
http_status_code, default_message = 407, "Proxy Authentication Required"
class RequestTimeout(HttpJsonError):
http_status_code, default_message = 408, "Request Timeout"
class Conflict(HttpJsonError):
http_status_code, default_message = 409, "Conflict"
class Gone(HttpJsonError):
http_status_code, default_message = 410, "Gone"
class LengthRequired(HttpJsonError):
http_status_code, default_message = 411, "Length Required"
class PreconditionFailed(HttpJsonError):
http_status_code, default_message = 412, "Precondition Failed"
class RequestEntityTooLarge(HttpJsonError):
http_status_code, default_message = 413, "Request Entity Too Large"
class RequestUriTooLong(HttpJsonError):
http_status_code, default_message = 414, "Request URI Too Long"
class UnsupportedMediaType(HttpJsonError):
http_status_code, default_message = 415, "Unsupported Media Type"
class RequestedRangeNotSatisfiable(HttpJsonError):
http_status_code, default_message = 416, "Requested Range Not Satisfiable"
class ExpectationFailed(HttpJsonError):
http_status_code, default_message = 417, "Expectation Failed"
class UnprocessableEntity(HttpJsonError):
http_status_code, default_message = 422, "Unprocessable Entity"
class TooManyRequests(HttpJsonError):
http_status_code, default_message = 429, "Too Many Requests"
class InternalError(HttpJsonError):
http_status_code, default_message = 500, "Internal Server Error"
class NotImplemented(HttpJsonError):
http_status_code, default_message = 501, "Not Implemented"
class BadGateway(HttpJsonError):
http_status_code, default_message = 502, "Bad Gateway"
class ServiceUnavailable(HttpJsonError):
http_status_code, default_message = 503, "Service Unavailable"
class GatewayTimeout(HttpJsonError):
http_status_code, default_message = 504, "Gateway Timeout"
|