-
Notifications
You must be signed in to change notification settings - Fork 115
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
Assimilate status code from non-error objects #37
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll want to wait a while to get feedback from everyone, but I would at least expect it to pass the current tests. You didn't describe (to me) a really breaking change, but yet the tests are broken, so I'm not sure if your implementation or your description represents what behavior you are expecting, if you can clarify.
It could be a breaking change, depending...
I've gone ahead and changed the code to adhere to the former option, because that makes more sense as desired behavior to me. However, this differs from current behavior for The latter option would be a breaking change but would be consistent with current behavior for Error objects. (Though, again, I almost consider it a bug.)
|
It isn't a bug; you can see from the tests it is the current intended behavior. |
None of the existing tests ensure the behavior as described. (I "fixed" the behavior without touching the tests, and they're still green.) |
Ah, I see. I looked at the test I was thinking and you're right. |
Opened #38 to determine if the Error behavior is intended or a bug. |
I'm fine either way, but need to get the Koa folks engaged here, as this module is one of thier top level APIs. |
for sure. And I'll add some new tests soon. All the existing tests are green except the node 0.6 job fails when attempting to install/compile node via nvm. |
Need to set dist to precise as of a month ago. I can push that fix to master and you can rebase your branch. To get it 😄 |
rebased. tests green! (with added tests) |
Hi @jasonkarns I'm honestly I just let this drop off the face of GitHub. I'm looking to release a new version of the module and so just trying to clean up issues / PR when I noticed it. I went ahead and squash + rebased your branch so it was up-to-date. Reading back through the comments trying to refresh my memory, I guess it looks like part of the reason this has been sitting is because it has a semver-major change included. That's actually fine because I am looking to bump the major version of this module very soon to drop Node.js 0.6 support anyway, so easy enough to go ahead and include this PR with that 👍 So I guess the only questions / comments I have are
|
Yep still looks right to me.
Thanks! |
The createError factory is already fairly versatile. It accepts Error objects and assimilates their name/status/stack. It accepts numbers and strings (taking the appropriate status). And it also accepts objects, assimilating their properties.
However, when given an object that contains status or statusCode, it does not assimilate that value.
My particular use case is for errors that are received from various api libraries, particularly REST APIs and Swagger clients. These objects do not inherit from
Error
, but the quite frequently have astatus
property.If this change is accepted, then the following errorware would be quite versatile indeed.
It would properly support the following middleware scenarios:
next(404)
-> would become http 404next("Some Error")
-> would become an http 500next(new Error("foo"))
-> would become http 500, with useful stack tracenext({status: 403, foo: "some error object thrown by an api client or library"})
-> would become http 403, assimilating other propsThe first three use-cases are already supported by http-errors. The last scenario would make createErrors consistent across all types of input.
I currently accomplish it via:
createError(err.status || err.statusCode, err)
which handles all the use cases describe above. (Though it relies on the now deprecated usage of number as second argument.)I can add tests if this change would be considered welcome.