21
21
22
22
import time
23
23
24
- from fastapi import FastAPI , Request , HTTPException
24
+ from fastapi import FastAPI , Request , HTTPException , status
25
25
from fastapi .middleware .cors import CORSMiddleware
26
26
from fastapi .responses import JSONResponse
27
+ from fastapi .exceptions import RequestValidationError
27
28
from sqlalchemy .exc import SQLAlchemyError
28
29
from uvicorn .main import Server as UvicornServer
29
30
@@ -87,54 +88,54 @@ def handle_exit(*args, **kwargs):
87
88
88
89
@app .exception_handler (ControllerError )
89
90
async def controller_error_handler (request : Request , exc : ControllerError ):
90
- log .error (f"Controller error: { exc } " )
91
+ log .error (f"Controller error in { request . url . path } ( { request . method } ) : { exc } " )
91
92
return JSONResponse (
92
- status_code = 409 ,
93
+ status_code = status . HTTP_409_CONFLICT ,
93
94
content = {"message" : str (exc )},
94
95
)
95
96
96
97
97
98
@app .exception_handler (ControllerTimeoutError )
98
99
async def controller_timeout_error_handler (request : Request , exc : ControllerTimeoutError ):
99
- log .error (f"Controller timeout error: { exc } " )
100
+ log .error (f"Controller timeout error in { request . url . path } ( { request . method } ) : { exc } " )
100
101
return JSONResponse (
101
- status_code = 408 ,
102
+ status_code = status . HTTP_408_REQUEST_TIMEOUT ,
102
103
content = {"message" : str (exc )},
103
104
)
104
105
105
106
106
107
@app .exception_handler (ControllerUnauthorizedError )
107
108
async def controller_unauthorized_error_handler (request : Request , exc : ControllerUnauthorizedError ):
108
- log .error (f"Controller unauthorized error: { exc } " )
109
+ log .error (f"Controller unauthorized error in { request . url . path } ( { request . method } ) : { exc } " )
109
110
return JSONResponse (
110
- status_code = 401 ,
111
+ status_code = status . HTTP_401_UNAUTHORIZED ,
111
112
content = {"message" : str (exc )},
112
113
)
113
114
114
115
115
116
@app .exception_handler (ControllerForbiddenError )
116
117
async def controller_forbidden_error_handler (request : Request , exc : ControllerForbiddenError ):
117
- log .error (f"Controller forbidden error: { exc } " )
118
+ log .error (f"Controller forbidden error in { request . url . path } ( { request . method } ) : { exc } " )
118
119
return JSONResponse (
119
- status_code = 403 ,
120
+ status_code = status . HTTP_403_FORBIDDEN ,
120
121
content = {"message" : str (exc )},
121
122
)
122
123
123
124
124
125
@app .exception_handler (ControllerNotFoundError )
125
126
async def controller_not_found_error_handler (request : Request , exc : ControllerNotFoundError ):
126
- log .error (f"Controller not found error: { exc } " )
127
+ log .error (f"Controller not found error in { request . url . path } ( { request . method } ) : { exc } " )
127
128
return JSONResponse (
128
- status_code = 404 ,
129
+ status_code = status . HTTP_404_NOT_FOUND ,
129
130
content = {"message" : str (exc )},
130
131
)
131
132
132
133
133
134
@app .exception_handler (ControllerBadRequestError )
134
135
async def controller_bad_request_error_handler (request : Request , exc : ControllerBadRequestError ):
135
- log .error (f"Controller bad request error: { exc } " )
136
+ log .error (f"Controller bad request error in { request . url . path } ( { request . method } ) : { exc } " )
136
137
return JSONResponse (
137
- status_code = 400 ,
138
+ status_code = status . HTTP_400_BAD_REQUEST ,
138
139
content = {"message" : str (exc )},
139
140
)
140
141
@@ -143,7 +144,7 @@ async def controller_bad_request_error_handler(request: Request, exc: Controller
143
144
async def compute_conflict_error_handler (request : Request , exc : ComputeConflictError ):
144
145
log .error (f"Controller received error from compute for request '{ exc .url ()} ': { exc } " )
145
146
return JSONResponse (
146
- status_code = 409 ,
147
+ status_code = status . HTTP_409_CONFLICT ,
147
148
content = {"message" : str (exc )},
148
149
)
149
150
@@ -160,12 +161,21 @@ async def http_exception_handler(request: Request, exc: HTTPException):
160
161
161
162
@app .exception_handler (SQLAlchemyError )
162
163
async def sqlalchemry_error_handler (request : Request , exc : SQLAlchemyError ):
163
- log .error (f"Controller database error: { exc } " )
164
+ log .error (f"Controller database error in { request . url . path } ( { request . method } ) : { exc } " )
164
165
return JSONResponse (
165
- status_code = 500 ,
166
+ status_code = status . HTTP_500_INTERNAL_SERVER_ERROR ,
166
167
content = {"message" : "Database error detected, please check logs to find details" },
167
168
)
168
169
170
+
171
+ @app .exception_handler (RequestValidationError )
172
+ async def validation_exception_handler (request : Request , exc : RequestValidationError ):
173
+ log .error (f"Request validation error in { request .url .path } ({ request .method } ): { exc } " )
174
+ return JSONResponse (
175
+ status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
176
+ content = {"message" : str (exc )}
177
+ )
178
+
169
179
# FIXME: do not use this middleware since it creates issue when using StreamingResponse
170
180
# see https://starlette-context.readthedocs.io/en/latest/middleware.html#why-are-there-two-middlewares-that-do-the-same-thing
171
181
0 commit comments