PERDYPTO commited on
Update app/main.py
Browse files- app/main.py +36 -0
app/main.py
CHANGED
|
@@ -36,6 +36,42 @@ app.add_middleware(
|
|
| 36 |
allow_headers=["*"],
|
| 37 |
)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
@app.get("/health")
|
| 41 |
async def health():
|
|
|
|
| 36 |
allow_headers=["*"],
|
| 37 |
)
|
| 38 |
|
| 39 |
+
@app.exception_handler(HTTPException)
|
| 40 |
+
async def http_exception_handler(request, exc):
|
| 41 |
+
detail = exc.detail
|
| 42 |
+
|
| 43 |
+
if isinstance(detail, dict):
|
| 44 |
+
code = detail.get("code", "INVALID_REQUEST")
|
| 45 |
+
message = detail.get("message", "Invalid request")
|
| 46 |
+
else:
|
| 47 |
+
code = "INVALID_REQUEST"
|
| 48 |
+
message = str(detail)
|
| 49 |
+
|
| 50 |
+
return JSONResponse(
|
| 51 |
+
status_code=exc.status_code,
|
| 52 |
+
content={
|
| 53 |
+
"success": False,
|
| 54 |
+
"error": {
|
| 55 |
+
"code": code,
|
| 56 |
+
"message": message,
|
| 57 |
+
},
|
| 58 |
+
},
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@app.exception_handler(RequestValidationError)
|
| 63 |
+
async def request_validation_exception_handler(request, exc):
|
| 64 |
+
return JSONResponse(
|
| 65 |
+
status_code=400,
|
| 66 |
+
content={
|
| 67 |
+
"success": False,
|
| 68 |
+
"error": {
|
| 69 |
+
"code": "INVALID_REQUEST",
|
| 70 |
+
"message": "Malformed or missing request fields",
|
| 71 |
+
},
|
| 72 |
+
},
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
|
| 76 |
@app.get("/health")
|
| 77 |
async def health():
|