Skip to content

Conversation

@davidjamesstone
Copy link
Contributor

@davidjamesstone davidjamesstone commented Oct 2, 2025

This should fix missing stack traces when an unhandled exception is encountered in a route handler.

More info

Hi all 👋

I'm looking for a way to capture and log stack traces when an unhandled exception is encountered in a route.

I have a generic onPreResponse handler for error logging that looks something like:

server.ext('onPreResponse', (request, h) => {
  const response = request.response

  if (Boom.isBoom(response)) {
    // Stack trace is undefined for unhandled system errors
    console.log(response.stack)
    
    return h.response({}).code(500)
  }
  
  return h.continue
})

If I have a route that generates an unhandled error e.g.

{
    method: 'GET',
    path: '/generate-error',
    async handler(request, h) {
      const o = undefined
      
      // Generate a TypeError
      const val = o.val
      
      return { some: 'thing' }
    }
}

The response.stack in onPreResponse is always undefined.

Stepping through the code it is because of this line in toolkit.js uses Boom.badImplementation which is stripping out the stack here in Hoek.clone

Is this by design? Has anyone overcome this and found a way to access the err.stack in an onPreResponse for unhandled system errors?

TIA

djs — 11:25 AM
Hoek is stripping it here when cloning the error, it gets the descriptor with key "stack" and redefines the property on the cloned error Object.defineProperty(newObj, key, descriptor) but the descriptor value is lost

djs — 12:16 PM
Looking at the most recent version of Hoek (11.0.7) it looks like stack now gets special handling (in node 21+). If I force Boom to use Hoek version 11.0.7, I see a stack trace in onPreResponse.

Latest Boom uses Hoek@^11.0.2.

Ok, I've now just seen this issue.
Given hapijs/hoek#390 is now merged, can we get a new version of Boom published? Or is it awaiting this?

djs — 1:12 PM
Node was installing [email protected] for boom. I fixed this by adding an explicit dependency to [email protected] in my package.json.

@davidjamesstone davidjamesstone changed the title Fix/df 531 error stack trace Fix/DF-531: Error stack traces Oct 2, 2025
@davidjamesstone davidjamesstone changed the title Fix/DF-531: Error stack traces fix/DF-531: Error stack traces Oct 2, 2025
@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 2, 2025

Comment on lines -43 to -50
const logLevel =
statusCode === StatusCodes.NOT_FOUND.valueOf() ? 'info' : 'error'
request.logger[logLevel](
response,
statusCode === StatusCodes.NOT_FOUND.valueOf()
? 'Resource not found'
: 'Unhandled error found'
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot, wonder why we never spotted this unreachable code before!

@davidjamesstone davidjamesstone merged commit dcdd3cf into main Oct 2, 2025
11 checks passed
@davidjamesstone davidjamesstone deleted the fix/DF-531-error-stack-trace branch October 2, 2025 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants