# HTTPException: 500 Internal Server Error

- **ID:** `python/starlette-httperror-internal-server-error`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

An unhandled exception occurred in the application code.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **Add exception handlers to log and return custom responses.** (90% success)
   ```
   from starlette.responses import JSONResponse
@app.exception_handler(500)
async def internal_error(request, exc):
    return JSONResponse({'error': 'Internal server error'}, status_code=500)
   ```
2. **Use middleware to catch unhandled exceptions.** (85% success)
   ```
   from starlette.middleware.base import BaseHTTPMiddleware
class ErrorHandlerMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next):
        try:
            response = await call_next(request)
        except Exception as e:
            return JSONResponse({'error': str(e)}, status_code=500)
        return response
   ```

## Dead Ends

- **Using a bare except clause that hides the error.** — Bare except catches all exceptions but doesn't log them, making debugging hard. (60% fail)
- **Returning a string without proper error handling.** — Returning a string doesn't prevent the 500 error; it only changes the response body. (50% fail)
