Find the real error
- Check the application's logs or error tracker (Sentry and similar) for the stack trace at the time of the request.
- On a hosting platform, open the function or runtime logs for the deployment, such as Vercel's Logs tab or
heroku logs --tail. - For PHP, look in the PHP error log; for Apache and nginx with PHP-FPM, also in the web server's error log.
- Reproduce the request locally with the same input. Many 500s only happen for one particular value.
Common causes
- An unhandled exception, such as reading a property of
undefinedornull. - A missing environment variable or secret in production that exists locally.
- The database is unreachable, a migration wasn't run, or a query fails.
- Unexpected input that the code assumed would never happen, such as an empty list or a missing field.
- A dependency that behaves differently in production, for example a different Node.js version.
Return safe errors from your API
Catch errors in one place, log the details on the server, and send the client a generic message with an ID it can quote:
// Express error handler, registered after all routes
app.use((error, request, response, next) => {
const id = crypto.randomUUID();
console.error(id, error);
response.status(500).json({ error: "Something went wrong on our side.", id });
});Use a more specific code when you can
If the client sent bad input, return a 4xx code such as 400 or 422 instead of letting the code crash into a 500. If a dependency is down, 502, 503 or 504 tells clients and monitoring more than a plain 500.
Frequently asked questions
Is a 500 error my fault as a visitor?
No. It's a problem on the server. Reload after a while, and if it keeps happening, let the site's owners know what you were doing.
What is the difference between 500 and 502?
500 comes from the application itself. 502 comes from a proxy or load balancer in front of it that couldn't get a valid response from the application.
Should I show the error details to users?
No. Stack traces and error messages can reveal file paths, queries and secrets. Log them on the server and show a generic message with a reference ID.
More http status code lookup guides
- 502 Bad Gateway: What It Means and How to Fix It
- 504 Gateway Timeout: Causes and Fixes
- 429 Too Many Requests: Meaning, Retry-After and Fixes
- 403 Forbidden: Meaning, Causes and How to Fix It
- 401 Unauthorized: What It Means and How to Fix It
- 503 Service Unavailable: Causes and Fixes
- 400 Bad Request: Causes and Fixes (Including Cookie Too Large)
- 404 Not Found: What It Means and How to Fix It
- 301 Moved Permanently: Meaning, SEO and Examples
- 302 Found: Temporary Redirects Explained
- 408 Request Timeout: Causes and How to Fix It