Skip to content

Commit 4714298

Browse files
committed
flask error handler
1 parent 8f690a1 commit 4714298

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

frameworks/flask_snippets/errorhandler_app/app.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import Flask
1+
from flask import Flask, abort
22
app = Flask(__name__)
33

44

@@ -9,11 +9,45 @@ def app_404(e):
99
return 'page not found', 404
1010

1111

12+
@app.errorhandler(500)
13+
def app_500(e):
14+
print(e)
15+
print(type(e))
16+
return 'page error', 500
17+
18+
19+
@app.errorhandler(ZeroDivisionError)
20+
def app_500(e):
21+
print(e)
22+
print(type(e))
23+
return 'ZeroDivisionError', 500
24+
25+
26+
@app.errorhandler(ZeroDivisionError)
27+
def app_500(e):
28+
print(e)
29+
print(type(e))
30+
return 'ZeroDivisionError', 500
31+
32+
33+
1234
@app.route("/")
1335
def hello():
1436
return "Hello World!"
1537

1638

39+
@app.route("/500")
40+
def error_500():
41+
a = 100 / 0
42+
return f'hello {a}'
43+
44+
45+
@app.route("/r500")
46+
def error_r500():
47+
abort(500)
48+
return 'hello'
49+
50+
1751
def main():
1852
app.run(debug=True)
1953

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from flask import Flask, abort, jsonify
2+
from werkzeug.exceptions import HTTPException, default_exceptions
3+
4+
app = Flask('test')
5+
6+
7+
@app.errorhandler(Exception)
8+
def handle_error(e):
9+
code = 500
10+
if isinstance(e, HTTPException):
11+
code = e.code
12+
return jsonify(error=str(e)), code
13+
14+
15+
for ex in default_exceptions:
16+
app.register_error_handler(ex, handle_error)
17+
18+
19+
@app.route('/')
20+
def index():
21+
return 'hello world'
22+
23+
24+
@app.route("/500")
25+
def error_500():
26+
a = 100 / 0
27+
return f'hello {a}'
28+
29+
30+
@app.route("/r500")
31+
def error_r500():
32+
abort(500)
33+
return 'hello'
34+
35+
36+
def main():
37+
app.run(debug=True)
38+
39+
40+
if __name__ == '__main__':
41+
main()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from functools import wraps
2+
3+
from flask import Flask, abort, jsonify
4+
from werkzeug.exceptions import HTTPException
5+
6+
app = Flask(__name__)
7+
8+
9+
@app.errorhandler(Exception)
10+
def handle_error(e):
11+
code = 500
12+
if isinstance(e, HTTPException):
13+
code = e.code
14+
return jsonify(error=str(e)), code
15+
16+
17+
def get_http_exception_handler(app):
18+
"""Overrides the default http exception handler to return JSON."""
19+
handle_http_exception = app.handle_http_exception
20+
@wraps(handle_http_exception)
21+
def ret_val(exception):
22+
exc = handle_http_exception(exception)
23+
return jsonify(error=exc.description), exc.code
24+
return ret_val
25+
26+
27+
# Override the HTTP exception handler.
28+
app.handle_http_exception = get_http_exception_handler(app)
29+
30+
31+
@app.route('/')
32+
def index():
33+
return 'hello world'
34+
35+
36+
@app.route("/500")
37+
def error_500():
38+
a = 100 / 0
39+
return f'hello {a}'
40+
41+
42+
@app.route("/r500")
43+
def error_r500():
44+
abort(500)
45+
return 'hello'
46+
47+
48+
def main():
49+
app.run(debug=True)
50+
51+
52+
if __name__ == '__main__':
53+
main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## refs
2+
3+
- [python - Global error handler for any exception - Stack Overflow](https://stackoverflow.com/questions/29332056/global-error-handler-for-any-exception)
4+
- [blueprint error handler not working?? · Issue #1935 · pallets/flask](https://github.com/pallets/flask/issues/1935)
5+
- [Allow blueprint-local routing for errorhandlers · Issue #503 · pallets/flask](https://github.com/pallets/flask/issues/503)

0 commit comments

Comments
 (0)