-
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
Consider replacing error factories with real constructors #11
Comments
Breaking this is not acceptable, so we'd only consider solutions that do not break that functionality, but I'm all-ears on solutions as long as |
I have no idea how |
|
So it's basically a question of being compatible with Unless someone has a brilliant idea, feel free to close this issue then. |
You can support both with (this is just a simplified example): function NotFoundError(message) {
var err = new Error(message)
err.name = 'NotFoundError'
return err
}
var error = new NotFoundError('oops!') |
Nope, that wouldn't pass Also, |
Gotcha. I mean, |
P.S. I hoped on IRC since you seem to be active :) |
I'm on #jshttp. We could use gitter if you want to keep public logs, btw. It also supports Markdown unlike IRC ;) |
Wow, this has been open for a long time :) So as you may have seen, I just closed it with a commit. The commit doesn't exactly correlate with the title of the issue, but I feel like it correlates a bit with the ideas floating around to the underlying issue, which was using The commit replaces using |
Thanks @dougwilson. I appreciate the effort you're putting into these projects. I think the change is in spirit with this issue. |
No problem :) I'm trying to match through here and pick up all these things that just kind of fell off, haha. Hopefully everything will start to get closed now. If you do have any new or additional comments, always feel free to open an issue! |
I noticed the docs don't mention using the named error constructors directly (i.e. new NotFound()). Is there a reason why? also since they are factories it seems that |
The docs for using the constructor is here: https://github.com/jshttp/http-errors/blob/master/README.md#new-createerrorcode--namemsg |
Please open a new issue if you're having issues or need help instead of a 2 year closed issue. |
Currently the "constructors" are actually just factory functions and we have a total of three such factories in the code that do pretty much the same thing (
ClientError
,ServerError
and the exported function).The only reason
ServerError
andClientError
have any inheritance at all is that the__proto__
property of the new error is manipulated directly. This is incompatible with some possible environments (the double underscores are there for a reason).It would be nice if the two constructors were actual constructors (i.e. return
this
, not a retrofitted error). For an example of how this would work see [email protected].The obvious drawback is that
Object.prototype.toString
returns[object Object]
instead of[object Error]
andutil.isError
returnsfalse
(though instanceof checks pass as one would expect). The benefit is that this approach can be made to work in environments that don't support overriding__proto__
.The text was updated successfully, but these errors were encountered: