---
title: "Does your error page print a stack trace? Hide it from attackers"
description: "A stack trace shown to the user gives an attacker your server and library names and versions, and sometimes a place to inject. The OWASP fix: a global error handler that returns a generic message, with the details in server logs only."
url: https://ihsanalapsi.dev/articles/error-handling-stack-traces
lang: en
date: 2026-11-15
updated: 2026-11-15
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Does your error page print a stack trace? Hide it from attackers

Before an attacker tries anything on your site, they collect information: which server, which framework, which version. The easiest place to find it is an error page you forgot open, printing the full stack trace. This article closes that door with one error handler.

## Why is a stack trace a risk for your site?

A stack trace is the list of functions the code was running at the moment of the error, with file and library names. When it reaches the user, an attacker gets the names and versions of your server, framework and libraries, and sometimes learns where there is a place to inject.

[The OWASP Error Handling Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Error_Handling_Cheat_Sheet.html) starts here: an attack begins with a reconnaissance phase, and unhandled errors can assist an attacker in it and reveal a lot of information about the target. Its examples include a Java stack trace that exposed the Struts2 and Tomcat versions, and a database warning that exposed a file path on the server.

## What should the user see when an error happens?

The user should see a generic message that says nothing about how the site is built, and all the details go into the server log so you can investigate. The cheat sheet says the response must not hold any content that would reveal implementation details, and a stack trace is one of the plainest of those details.

So every error has two versions:


| For whom       | What it holds                                             |
| -------------- | --------------------------------------------------------- |
| The user       | A short generic message and the status code               |
| The server log | The full exception, the stack trace, and the request time |


## How do you build a global error handler?

A global error handler is one place in the app that catches every uncaught error, logs its details, and returns a generic response. The cheat sheet shows the idea in Spring and ASP.NET Core; here is the same idea in Express:

```javascript
app.use((err, req, res, next) => {
  logger.error({ err, path: req.path }, 'unhandled error');
  res.status(500).json({
    title: 'Something went wrong',
    status: 500,
  });
});
```

The response in this example has no `err.message` and no `err.stack`: those stay in the log.

## When do you return 500, and when 4xx?

500 is for server-side errors you did not expect. If the request itself is at fault, like unauthorized access or a body larger than allowed, the cheat sheet says a 4xx code may fit better.

It also advises watching the rate of 5xx responses, because a rise can mean the app is failing on certain inputs.

## Where do you start now?

The lesson for us as developers: an error has two audiences. The user gets one sentence, and you get the whole story in the log.

1. Open your site in production and request a wrong path on purpose, and check whether a stack trace, a framework name or a version shows up.
2. Add a global error handler, and make sure developer error pages run in development only.
3. Keep the generic response in the same shape everywhere, and watch the number of 5xx responses.

Error messages that reveal accounts are a close story, covered in [the article on error messages and accounts](/articles/account-enumeration-error-messages). The injection point an error points to is most often the database: [the parameterized queries article](/articles/sql-injection-parameterized-queries). And if you want someone to look for these leaks on your site the way an attacker would, that is what [Muhassan](/muhassan) does.

The last error on your site, what did the user see?

## FAQ

**Can I keep the detailed error page while I develop?**

Yes, only in development. The ASP.NET Core example in the OWASP cheat sheet turns on the global error handler in every environment other than DEV, because the debug page is useful during implementation.

**Do I always return 500 for any error?**

No. 500 is for server-side errors you did not expect. If the request itself is at fault, like unauthorized access or an oversized body, a 4xx code fits better.

**Is there a standard shape for API error responses?**

Yes. The cheat sheet points to RFC 7807, Problem Details for HTTP APIs, as a format for error responses, and its references list RFC 9457 with the same name.
