HTTP 500 Internal Server Error — Causes, Debugging & Fix Guide
An unexpected condition was encountered and the server cannot fulfil the request. This is a catch-all for server-side errors. The server should not include error details in the response body in production (to avoid leaking implementation details).
When to Return 500
Return 500 when an unexpected exception occurs that you haven't handled with a more specific status code. Always log the full error server-side. Return a generic error message to the client — never expose stack traces in production.
Common Causes
- Unhandled exception or promise rejection
- Database connection error
- Null pointer dereference or type error
- Out of memory error
- Third-party service returning unexpected data
HTTP Response Example
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
{"error": "Internal Server Error"}Code Examples
Express.js
// Global error handler (must have 4 parameters)
app.use((err, req, res, next) => {
console.error({
message: err.message,
stack: err.stack,
url: req.url,
method: req.method,
})
// Never expose stack traces in production
res.status(err.status ?? 500).json({
error: process.env.NODE_ENV === 'production'
? 'Internal Server Error'
: err.message,
})
})Next.js App Router
// app/error.tsx — catches runtime errors in the component tree
'use client'
export default function Error({ error, reset }) {
return (
<div>
<h2>Something went wrong</h2>
<button onClick={reset}>Try again</button>
</div>
)
}
// app/api/route.ts — wrap handlers in try/catch
export async function GET() {
try {
const data = await fetchData()
return Response.json(data)
} catch (err) {
console.error(err)
return Response.json({ error: 'Internal Server Error' }, { status: 500 })
}
}Related Status Codes
Frequently Asked Questions
What does HTTP 500 Internal Server Error mean?
An unexpected condition was encountered and the server cannot fulfil the request. This is a catch-all for server-side errors. The server should not include error details in the response body in production (to avoid leaking implementation details).
When should an API return 500?
Return 500 when an unexpected exception occurs that you haven't handled with a more specific status code. Always log the full error server-side. Return a generic error message to the client — never expose stack traces in production.
What causes an HTTP 500 error?
Common causes: Unhandled exception or promise rejection; Database connection error; Null pointer dereference or type error; Out of memory error; Third-party service returning unexpected data.
