HTTP status code lookup

500 Internal Server Error: Causes and How to Fix It

A 500 Internal Server Error means the server ran into a problem it didn't handle, usually an exception in the application code. It says nothing about the cause; that is in the server's logs.

As a visitor, there's little you can do but reload later. As the site owner, start with the logs.

500

Internal Server Error

The server hit an unexpected condition and couldn't complete the request. A generic catch-all error.

Look up any other status code →

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 undefined or null.
  • 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

Open the full http status code lookup →

From the blog