Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -34,7 +34,12 @@ from logger import log_error, log_info, log_warning
|
|
34 |
|
35 |
# Exception imports
|
36 |
from exceptions import (
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
get_http_status_code
|
39 |
)
|
40 |
from dotenv import load_dotenv
|
@@ -189,37 +194,49 @@ async def global_exception_handler(request: Request, exc: Exception):
|
|
189 |
})
|
190 |
)
|
191 |
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
@app.exception_handler(ValidationError)
|
195 |
-
async def
|
196 |
-
"""Handle
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
'message': error['msg'],
|
205 |
-
'type': error['type'],
|
206 |
-
'input': error.get('input')
|
207 |
-
})
|
208 |
-
|
209 |
-
log_warning(
|
210 |
-
"Validation error",
|
211 |
-
request_id=request_id,
|
212 |
-
errors=errors
|
213 |
)
|
214 |
-
|
|
|
|
|
|
|
215 |
return JSONResponse(
|
216 |
-
status_code=
|
217 |
content={
|
218 |
-
"
|
219 |
-
"
|
220 |
-
"
|
221 |
-
"
|
222 |
-
"timestamp": datetime.utcnow().isoformat()
|
223 |
}
|
224 |
)
|
225 |
|
|
|
34 |
|
35 |
# Exception imports
|
36 |
from exceptions import (
|
37 |
+
DuplicateResourceError,
|
38 |
+
ValidationError,
|
39 |
+
ResourceNotFoundError,
|
40 |
+
FlareException,
|
41 |
+
RaceConditionError,
|
42 |
+
format_error_response,
|
43 |
get_http_status_code
|
44 |
)
|
45 |
from dotenv import load_dotenv
|
|
|
194 |
})
|
195 |
)
|
196 |
|
197 |
+
@app.exception_handler(DuplicateResourceError)
|
198 |
+
async def duplicate_resource_handler(request: Request, exc: DuplicateResourceError):
|
199 |
+
"""Handle duplicate resource errors"""
|
200 |
+
return JSONResponse(
|
201 |
+
status_code=409,
|
202 |
+
content={
|
203 |
+
"detail": str(exc),
|
204 |
+
"error_type": "duplicate_resource",
|
205 |
+
"resource_type": exc.details.get("resource_type"),
|
206 |
+
"identifier": exc.details.get("identifier")
|
207 |
+
}
|
208 |
+
)
|
209 |
+
|
210 |
+
@app.exception_handler(RaceConditionError)
|
211 |
+
async def race_condition_handler(request: Request, exc: RaceConditionError):
|
212 |
+
"""Handle race condition errors"""
|
213 |
+
return JSONResponse(
|
214 |
+
status_code=409,
|
215 |
+
content=exc.to_http_detail()
|
216 |
+
)
|
217 |
|
218 |
@app.exception_handler(ValidationError)
|
219 |
+
async def validation_error_handler(request: Request, exc: ValidationError):
|
220 |
+
"""Handle validation errors"""
|
221 |
+
return JSONResponse(
|
222 |
+
status_code=422,
|
223 |
+
content={
|
224 |
+
"detail": str(exc),
|
225 |
+
"error_type": "validation_error",
|
226 |
+
"details": exc.details
|
227 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
)
|
229 |
+
|
230 |
+
@app.exception_handler(ResourceNotFoundError)
|
231 |
+
async def resource_not_found_handler(request: Request, exc: ResourceNotFoundError):
|
232 |
+
"""Handle resource not found errors"""
|
233 |
return JSONResponse(
|
234 |
+
status_code=404,
|
235 |
content={
|
236 |
+
"detail": str(exc),
|
237 |
+
"error_type": "resource_not_found",
|
238 |
+
"resource_type": exc.details.get("resource_type"),
|
239 |
+
"identifier": exc.details.get("identifier")
|
|
|
240 |
}
|
241 |
)
|
242 |
|