-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.py
211 lines (176 loc) · 6.67 KB
/
errors.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Flask Blueprint that contains the custom error handlers for SCS, making sure
that machine interpretable JSON responses are returned in case of errors.
Use the register() and register_exception() functions to configure the errors
module to respond with custom errors for your blueprints.
Copyright 2022 Tom Brouwer
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import re
import functools
from flask import Blueprint, jsonify, Response, Flask, current_app
from flask.blueprints import BlueprintSetupState
from werkzeug.exceptions import HTTPException
bp = Blueprint('errors', __name__)
_non_word_chars_regex = re.compile(r'\W+')
def _register(code: int, id_: str, message: str, *, app: Flask):
"""
Registers an error id for a specific HTTP response code. To trigger the
error, pass the id as a description to the flask abort command, e.g.:
`abort(429, description={'id': 'rate-limited'})`
Args:
code:
The HTTP response code to register the error for
id_:
A unique identifier, returned in the JSON response
message:
The message that should be returned in the JSON response
app:
The application to register the id to
Raises:
ValueError: In case the given id_ is already registered for the code
"""
code_errors = app.scs._error_definitions.setdefault(code, {})
if id_ in code_errors:
raise ValueError(
f'CONFLICT: id {id_} is already registered for code {code}',
)
code_errors[id_] = message
def _register_exception(
exception_class: type, id_: str, *, message: str = None, app: Flask,
):
"""
Registers a specific error id to be returned, if the given exception is
raised when handling a request. If the given id is not yet registered for
the 500 status code, also a message should be provided.
Note that if you don't want the server to return a 500 status code when
the exception occurs, you should handle it in your own code, and call the
'abort()' function, as described in the register() function docstring.
Args:
exception_class:
The class of the exception for which, if it occurs, the error with
the given id should be returned
id_:
An error id, that is either (1) already registered for code 500
using the register() function, or (2) a new error id. In case of
the latter, also provide a message
message:
Provide a message in case the given id is not yet registered for
the 500 response code
app:
The app to register the exception on
Raises:
ValueError:
In case the given Exception or id/message combination is already
registered
"""
for existing_class, _ in app.scs._exception_ids:
if existing_class == exception_class:
raise ValueError(
f'CONFLICT: Exception {exception_class.__name__} is already '
f'linked to id {id_}',
)
if message is not None:
_register(500, id_, message, app=app)
elif id_ not in app.scs._error_definitions[500]:
raise ValueError(
f'Error id {id_} not registered as an error, so a message must'
' be provided'
)
app.scs._exception_ids.append((exception_class, id_))
@bp.record
def init(setup_state: BlueprintSetupState):
"""Initialize the errors module"""
setup_state.app.scs._error_definitions = {
400: {
'bad-request': (
'Your request is invalid. For POST requests, check if the '
'request body contains valid JSON'
)
},
401: {
'unauthenticated': 'Invalid authentication credentials provided',
},
403: {
'unauthorized': (
'You are not authorized to access this resource'
),
},
405: {
'method-not-allowed': (
'The HTTP request method cannot be used on this endpoint'
),
},
500: {
'internal-server-error': (
'An internal server error occured'
)
}
}
setup_state.app.scs._exception_ids = []
setup_state.app.scs.register_error = functools.partial(
_register, app=setup_state.app
)
setup_state.app.scs.register_exception = functools.partial(
_register_exception, app=setup_state.app
)
def _error_response(code: int, id_: str, message: str) -> tuple[Response, int]:
"""
Returns an SCS error response and the corresponding code
Args:
code: Status code to return
id_: Error id to return
message: Error message to return
Returns:
The Flask response from the jsonify function and the response code
"""
return jsonify({
'error': {
'id': id_,
'message': message,
}
}), code
def _get_500_error_id(error: Exception) -> str:
"""
Returns the error id to use for the given exception
"""
id_ = next(iter(current_app.scs._error_definitions[500].keys()))
for exception_cls, error_id in current_app.scs._exception_ids:
if isinstance(error.original_exception, exception_cls):
id_ = error_id
break
return id_
@bp.app_errorhandler(HTTPException)
def json_error_response(e) -> tuple[Response, int]:
"""
Application-wide error handler to generate a JSON response in case of
errors
Args:
e: The exception that triggered the handler
Returns:
The JSON response and status code to respond with to the user
"""
if e.code == 500:
id_ = _get_500_error_id(e)
message = current_app.scs._error_definitions[e.code][id_]
elif e.code in current_app.scs._error_definitions:
id_ = next(iter(current_app.scs._error_definitions[e.code].keys()))
if isinstance(e.description, dict):
id_ = e.description.get('id', id_)
message = current_app.scs._error_definitions[e.code][id_]
else:
id_ = _non_word_chars_regex.sub('-', e.name.lower())
message = e.description
return _error_response(
e.code,
id_,
message,
)