| 172 | |
| 173 | @app.exception_handler(Exception) |
| 174 | async def generic_error_handler(request: Request, exc: Exception): |
| 175 | # Log the actual error for debugging |
| 176 | log.error(f"Unhandled error: {exc}", exc_info=True) |
| 177 | print(f"Unhandled error: {exc}") |
| 178 | |
| 179 | # Print the stack trace |
| 180 | print(f"Stack trace: {exc}") |
| 181 | if (os.getenv("SENTRY_DSN") is not None) and (os.getenv("SENTRY_DSN") != ""): |
| 182 | import sentry_sdk |
| 183 | |
| 184 | sentry_sdk.capture_exception(exc) |
| 185 | |
| 186 | return JSONResponse( |
| 187 | status_code=500, |
| 188 | content={ |
| 189 | "detail": "An internal server error occurred", |
| 190 | # Only include error details in debug/development mode |
| 191 | # "debug_info": str(exc) if settings.debug else None |
| 192 | }, |
| 193 | ) |
| 194 | |
| 195 | @app.exception_handler(NoResultFound) |
| 196 | async def no_result_found_handler(request: Request, exc: NoResultFound): |