Uh oh!
There was an error while loading. Please reload this page.
Assimilate status code from non-error objects - #37
Conversation
dougwilson
left a comment
There was a problem hiding this comment.
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.
jasonkarns
commented
Sep 2, 2017
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.) |
dougwilson
commented
Sep 2, 2017
It isn't a bug; you can see from the tests it is the current intended behavior. |
jasonkarns
commented
Sep 2, 2017
None of the existing tests ensure the behavior as described. (I "fixed" the behavior without touching the tests, and they're still green.) |
dougwilson
commented
Sep 2, 2017
Ah, I see. I looked at the test I was thinking and you're right. |
jasonkarns
commented
Sep 2, 2017
Opened #38 to determine if the Error behavior is intended or a bug. |
dougwilson
commented
Sep 2, 2017
I'm fine either way, but need to get the Koa folks engaged here, as this module is one of thier top level APIs. |
jasonkarns
commented
Sep 2, 2017
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. |
dougwilson
commented
Sep 2, 2017
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) |
dougwilson
commented
Jul 18, 2018
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
|
jasonkarns
commented
Jul 18, 2018
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 astatusproperty.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.