@@ -4,21 +4,21 @@ Exception Handling
4
4
Connexion allows you to register custom error handlers to convert Python ``Exceptions `` into HTTP
5
5
problem responses.
6
6
7
+ You can register error handlers on:
8
+
9
+ - The exception class to handle
10
+ If this exception class is raised somewhere in your application or the middleware stack,
11
+ it will be passed to your handler.
12
+ - The HTTP status code to handle
13
+ Connexion will raise ``starlette.HTTPException `` errors when it encounters any issues
14
+ with a request or response. You can intercept these exceptions with specific status codes
15
+ if you want to return custom responses.
16
+
7
17
.. tab-set ::
8
18
9
19
.. tab-item :: AsyncApp
10
20
:sync: AsyncApp
11
21
12
- You can register error handlers on:
13
-
14
- - The exception class to handle
15
- If this exception class is raised somewhere in your application or the middleware stack,
16
- it will be passed to your handler.
17
- - The HTTP status code to handle
18
- Connexion will raise ``starlette.HTTPException `` errors when it encounters any issues
19
- with a request or response. You can intercept these exceptions with specific status codes
20
- if you want to return custom responses.
21
-
22
22
.. code-block :: python
23
23
24
24
from connexion import AsyncApp
@@ -40,17 +40,6 @@ problem responses.
40
40
.. tab-item :: FlaskApp
41
41
:sync: FlaskApp
42
42
43
- You can register error handlers on:
44
-
45
- - The exception class to handle
46
- If this exception class is raised somewhere in your application or the middleware stack,
47
- it will be passed to your handler.
48
- - The HTTP status code to handle
49
- Connexion will raise ``starlette.HTTPException `` errors when it encounters any issues
50
- with a request or response. The underlying Flask application will raise
51
- ``werkzeug.HTTPException `` errors. You can intercept both of these exceptions with
52
- specific status codes if you want to return custom responses.
53
-
54
43
.. code-block :: python
55
44
56
45
from connexion import FlaskApp
@@ -69,20 +58,34 @@ problem responses.
69
58
.. automethod :: connexion.FlaskApp.add_error_handler
70
59
:noindex:
71
60
72
- .. tab-item :: ConnexionMiddleware
73
- :sync: ConnexionMiddleware
61
+ .. note ::
62
+
63
+ .. warning ::
64
+
65
+ ⚠️ **The following is not recommended as it complicates the exception handling logic, **
74
66
75
- You can register error handlers on:
67
+ You can also register error handlers on the underlying flask application directly.
76
68
77
- - The exception class to handle
78
- If this exception class is raised somewhere in your application or the middleware stack,
79
- it will be passed to your handler.
80
- - The HTTP status code to handle
81
- Connexion will raise ``starlette.HTTPException `` errors when it encounters any issues
82
- with a request or response. You can intercept these exceptions with specific status codes
83
- if you want to return custom responses.
84
- Note that this might not catch ``HTTPExceptions `` with the same status code raised by
85
- your wrapped ASGI/WSGI framework.
69
+ .. code-block :: python
70
+
71
+ flask_app = app.app
72
+ flask_app.register_error_handler(FileNotFoundError , not_found)
73
+ flask_app.register_error_handler(404 , not_found)
74
+
75
+ `Flask documentation `_
76
+
77
+ Error handlers registered this way:
78
+
79
+ - Will only intercept exceptions thrown in the application, not in the Connexion
80
+ middleware.
81
+ - Can intercept exceptions before they reach the error handlers registered on the
82
+ connexion app.
83
+ - When registered on status code, will intercept only
84
+ ``werkzeug.exceptions.HTTPException `` thrown by werkzeug / Flask not
85
+ ``starlette.exceptions.HTTPException ``.
86
+
87
+ .. tab-item :: ConnexionMiddleware
88
+ :sync: ConnexionMiddleware
86
89
87
90
.. code-block :: python
88
91
@@ -105,10 +108,17 @@ problem responses.
105
108
.. automethod :: connexion.ConnexionMiddleware.add_error_handler
106
109
:noindex:
107
110
111
+ .. note ::
112
+
113
+ This might not catch ``HTTPExceptions `` with the same status code raised by
114
+ your wrapped ASGI/WSGI framework.
115
+
108
116
.. note ::
109
117
110
118
Error handlers can be ``async `` coroutines as well.
111
119
120
+ .. _Flask documentation : https://flask.palletsprojects.com/en/latest/errorhandling/#error-handlers
121
+
112
122
Default Exception Handling
113
123
--------------------------
114
124
By default connexion exceptions are JSON serialized according to
0 commit comments