Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error handler #24

Open
kraiovsky opened this issue Jul 25, 2019 · 1 comment
Open

Custom error handler #24

kraiovsky opened this issue Jul 25, 2019 · 1 comment

Comments

@kraiovsky
Copy link

kraiovsky commented Jul 25, 2019

Hi,

let's say I throw an error in the route handler:

const handler = () => async (ctx, next) => {
  try {
    // do something
    await next()
  } catch (err) {
    ctx.throw(500, err)
  }
}

and handle the error with a global middleware:

const errorHandler = () => async (ctx, next) => {
  try {
    await next()
  } catch (err) {
    ctx.status = err.status
    ctx.message = 'error message'
    ctx.body = 'error body'
  }
}

Pino would not log original ctx.throw and log INFO request complete with req and res objects, but no error itself.

Q:

  1. how do I add error to this event?
  2. alternatively, if I log the error myself with ctx.logger.error, how to enrich it with other meta data i.e. timestamp, context etc.?

thank you!

UPDATE to (2)
Apparently, if logged ctx.logger.error({ err }) it would add req and more meta and context around an error.
Was it documented somewhere that I missed and is worth documenting?

Would it be the "proper" way to log errors in custom error handler?

@ylavish
Copy link

ylavish commented Jun 1, 2023

@kraiovsky Did you find any workaround to this?

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

No branches or pull requests

2 participants