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

httpError(N, error) is misleading if error is already an HTTP error #96

Open
innermatrix opened this issue May 22, 2023 · 2 comments
Open
Labels

Comments

@innermatrix
Copy link

I would expect

httpError(500, httpError(402)).statusCode

to be 500, but it's 402.

(The use case here is that I have a try/catch statement that turns all exceptions into HTTP 500s by default, and it's not working correctly when the original exception is already an httpError.)

@dougwilson
Copy link
Contributor

dougwilson commented May 22, 2023

Mmm, yes, that is good point. Unfortunately it is the current behavior relied upon by existing uses (that a given error object will have it's status code preserved over the argument status code). This is regardless of the error being httpError or not. So the docs are not clear on this, but I also lile the idea of how you would like it to behave too, so I think we should support both somehow. I will think on the API for that 👍

@jonchurch
Copy link
Member

jonchurch commented Mar 10, 2024

Currently createErrors has a very loose args interface.

I was surprised this issue was possible, because we will ignore any status passed in via the additional props bag. But because we are passing something which is instanceof Error, we will honor the status.

it doesn't matter what position we pass an error in, if it is detected in args we will set the status there.

So consider also:

const createErrors = require('http-errors')
const err = new Error()
err.statusCode = 411

const createdError = createErrors(
  createErrors(404),
  createErrors(414),
  { bag: 'foo', status: 500 },
  createErrors(400, createErrors(err)),
  { bag: 'foo', status: 429 },
  404
)

console.log(createdError.statusCode === 411) // true

The last instance of an Error wins. The properties bag uses the props but ignores status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants