This repository was archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
192 lines (147 loc) · 6.29 KB
/
__init__.py
File metadata and controls
192 lines (147 loc) · 6.29 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from flask import Flask
from flask import Response
from flask_utils.errors._error_template import _generate_error_json
from flask_utils.errors.badrequest import BadRequestError
from flask_utils.errors.conflict import ConflictError
from flask_utils.errors.failed_dependency import FailedDependencyError
from flask_utils.errors.forbidden import ForbiddenError
from flask_utils.errors.gone import GoneError
from flask_utils.errors.notfound import NotFoundError
from flask_utils.errors.origin_is_unreachable import OriginIsUnreachableError
from flask_utils.errors.service_unavailable import ServiceUnavailableError
from flask_utils.errors.unauthorized import UnauthorizedError
from flask_utils.errors.unprocessableentity import UnprocessableEntityError
from flask_utils.errors.web_server_is_down import WebServerIsDownError
def _register_error_handlers(application: Flask) -> None:
"""
This function will register all the error handlers for the application
:param application: The Flask application to register the error handlers
:return: None
.. versionchanged:: 0.5.0
Made the function private. If you want to register the custom error handlers, you need to
pass `register_error_handlers=True` to the :class:`~flask_utils.extension.FlaskUtils` class
or to :meth:`~flask_utils.extension.FlaskUtils.init_app`
.. code-block:: python
from flask import Flask
from flask_utils import FlaskUtils
app = Flask(__name__)
utils = FlaskUtils(app)
# OR
utils = FlaskUtils()
utils.init_app(app)
.. versionadded:: 0.1.0
"""
@application.errorhandler(BadRequestError)
def generate_badrequest(error: BadRequestError) -> Response:
"""
This is the 400 response creator. It will create a 400 response along with
a custom message and the 400 code
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 400)
@application.errorhandler(ConflictError)
def generate_conflict(error: ConflictError) -> Response:
"""
This is the 409 response creator. It will create a 409 response along with
a custom message and the 409 code
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 409)
@application.errorhandler(ForbiddenError)
def generate_forbidden(error: ForbiddenError) -> Response:
"""
This is the 403 response creator. It will create a 403 response along with
a custom message and the 403 code
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 403)
@application.errorhandler(NotFoundError)
def generate_notfound(error: NotFoundError) -> Response:
"""
This is the 404 response creator. It will create a 404 response with
a custom message and the 404 code.
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 404)
@application.errorhandler(UnauthorizedError)
def generate_unauthorized(error: UnauthorizedError) -> Response:
"""
This is the 401 response creator. It will create a 401 response with
a custom message and the 401 code.
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 401)
@application.errorhandler(OriginIsUnreachableError)
def generate_origin_is_unreachable(error: OriginIsUnreachableError) -> Response:
"""
This is the 523 response creator. It will create a 523 response with
a custom message and the 523 code.
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 523)
@application.errorhandler(WebServerIsDownError)
def generate_web_server_is_down(error: WebServerIsDownError) -> Response:
"""
This is the 521 response creator. It will create a 521 response with
a custom message and the 521 code.
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 521)
@application.errorhandler(FailedDependencyError)
def generate_failed_dependency(error: FailedDependencyError) -> Response:
"""
This is the 424 response creator. It will create a 424 response with
a custom message and the 424 code.
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 424)
@application.errorhandler(GoneError)
def generate_gone(error: GoneError) -> Response:
"""
This is the 410 response creator. It will create a 410 response with
a custom message and the 410 code.
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 410)
@application.errorhandler(UnprocessableEntityError)
def generate_unprocessable_entity(error: UnprocessableEntityError) -> Response:
"""
This is the 422 response creator. It will create a 422 response with
a custom message and the 422 code.
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 422)
@application.errorhandler(ServiceUnavailableError)
def generate_service_unavailable(error: ServiceUnavailableError) -> Response:
"""
This is the 503 response creator. It will create a 503 response with
a custom message and the 503 code.
:param error: The error body
:return: Returns the response formatted
"""
return _generate_error_json(error, 503)
__all__ = [
"BadRequestError",
"ConflictError",
"ForbiddenError",
"NotFoundError",
"UnauthorizedError",
"_generate_error_json",
"FailedDependencyError",
"WebServerIsDownError",
"OriginIsUnreachableError",
"GoneError",
"UnprocessableEntityError",
"ServiceUnavailableError",
"_register_error_handlers",
]