Justin Schroeder
Justin Schroeder
NNuxt
Created by Justin Schroeder on 9/13/2024 in #❓・help
Custom nitro errorHandler, but maintain Nuxt’s error handling.
👋 Is it possible to define a custom eventHandler for nitro, but then still use nuxt’s error handler? We need access to the original event, so using the error hook is too "late" because (in prod) we lose the stack trace (https://github.com/nuxt/nuxt/blob/2466af53b0331cdb8b17c2c3b08675c5985deaf3/packages/nuxt/src/core/runtime/nitro/error.ts#L18-L21) which we need for our logging platform. We have defined a custom nitro error handler:
// nuxt.config.ts
{
...
nitro: {
errorHandler: '~/errorHandler.ts'
}
...
}
// nuxt.config.ts
{
...
nitro: {
errorHandler: '~/errorHandler.ts'
}
...
}
which works great for getting the original error object — however, we’d like to the invoke the default Nuxt error handling. in a perfect world our handler would just be a higher order function:
import { errorhandler } from '#magic-nuxt-import-thingy???'

export default defineNitroErrorHandler((error, event) => {

// Do our custom logging logic here with the origin H3Error event...

return errorhandler(error, event)
})
import { errorhandler } from '#magic-nuxt-import-thingy???'

export default defineNitroErrorHandler((error, event) => {

// Do our custom logging logic here with the origin H3Error event...

return errorhandler(error, event)
})
1 replies