File tree Expand file tree Collapse file tree 4 files changed +134
-1
lines changed
frameworks/flask_snippets/errorhandler_app Expand file tree Collapse file tree 4 files changed +134
-1
lines changed Original file line number Diff line number Diff line change 1
- from flask import Flask
1
+ from flask import Flask , abort
2
2
app = Flask (__name__ )
3
3
4
4
@@ -9,11 +9,45 @@ def app_404(e):
9
9
return 'page not found' , 404
10
10
11
11
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
+
12
34
@app .route ("/" )
13
35
def hello ():
14
36
return "Hello World!"
15
37
16
38
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
+
17
51
def main ():
18
52
app .run (debug = True )
19
53
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments