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

Preservation of `err.name` via `Boom.create` or `Boom[someMethod]` #138

Closed
niftylettuce opened this issue Oct 26, 2016 · 4 comments
Assignees
Labels
Milestone

Comments

@niftylettuce
Copy link
Contributor

@niftylettuce niftylettuce commented Oct 26, 2016

Shouldn't we be preserving the original err.name?

@niftylettuce

This comment has been minimized.

Copy link
Contributor Author

@niftylettuce niftylettuce commented Oct 26, 2016

Perhaps maybe detect if message is an err when passed to internals.create and if it is then wrap it instead? Would be really nice to have this so we could use Boom.badRequest(err).

const error = new Error(message ? message : undefined); // Avoids settings null message

@niftylettuce

This comment has been minimized.

Copy link
Contributor Author

@niftylettuce niftylettuce commented Oct 26, 2016

Currently the only way to do this is:

> Boom.wrap(err, 400);
{ ValidationError: Path is required
    at repl:1:11
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:313:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:513:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
  name: 'ValidationError',
  isBoom: true,
  isServer: false,
  data: null,
  output:
   { statusCode: 400,
     payload:
      { statusCode: 400,
        error: 'Bad Request',
        message: 'Path is required' },
     headers: {} },
  reformat: [Function] }
> Boom.wrap(err, 400).name
'ValidationError'
niftylettuce added a commit to ladjs/koa-better-error-handler that referenced this issue Oct 26, 2016
@niftylettuce

This comment has been minimized.

Copy link
Contributor Author

@niftylettuce niftylettuce commented Oct 26, 2016

See my pull request #139

@niftylettuce

This comment has been minimized.

Copy link
Contributor Author

@niftylettuce niftylettuce commented Oct 26, 2016

For anyone wondering why we need this and #139, the reason is to preserve error messages if you want to shorthand a method instead of having to remember status code. For example, if there's a validation error in Mongoose, you can simply ctx.throw(Boom.badRequest(err)) and if you also were to happen to use this https://github.com/niftylettuce/koa-better-error-handler as your error handler with Koa, then it will preserve the err.name for us to recognize the error as a ValidationError. Otherwise there's not really a clean way to do this. This is a minimal addition to boom that is fully tested and only adds two lines of code to the codebase. I have added tests to #139 as well to keep 100% coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.