-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patherror_handlers.py
30 lines (23 loc) · 911 Bytes
/
error_handlers.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
import json
from flask import Response, current_app, request
def register_errorhandlers(app):
def render_error(error):
extra = {
'flask': {
'endpoint': str(request.endpoint).lower(),
'view_args': request.view_args,
},
'error': {
'code': error.code,
"msg": "{message}".format(message = str(error))
}
}
message = 'Error request for "%s %s" from %s' % (request.method, request.url, extra.get('remote_addr', '-'))
current_app.logger.error(message, extra = extra)
return Response(json.dumps({
"code": error.code,
"msg": "{message}".format(message = str(error))
}), mimetype = "application/json", status = 400)
for errcode in [404, 500]:
app.errorhandler(errcode)(render_error)
return None