-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Should Node.js standard library throw custom Error subclasses? #8342
Comments
A couple of immediate thoughts:
|
The other stuff sounds good to me though. Being able to type-check errors would be fantastic. |
Related to this is my PR #6573, which begins attaching a stable error code to every error. I need to get through and get that PR rebased and updated but it should give an idea on the direction. |
@jasnell I like the error code idea too. I'd actually like to see both though. Error types let us group errors by category to handle them similarly. For example, there's multiple ways in which network access can error, so you may want the detail to see if retrying is a valid approach. However, in a higher level API, you may receive network errors alongside other non-network errors, so branching logic on that error type would be quite helpful. 😸 |
-1 to defining new error types. |
I just want to point out you can use stuff like I'm also -1 on this at this point since it seems like a lot of effort to replace a system that already works (the error codes). |
I would prefer a non-native Error subtype for Node's own errors, if anything for easier differentiation at a glance and a better ability to use them in testing (no need to reimplement Node's magic for a mock FS error). I am 👎 for a complex error hierarchy (that's just not very idiomatic, and it's unnecessarily complex), but something like just |
This issue has been inactive for sufficiently long that it seems like perhaps it should be closed. Feel free to re-open (or leave a comment requesting that it be re-opened) if you disagree. I'm just tidying up and not acting on a super-strong opinion or anything like that. |
@Trott Can this be re-opened please? At the very least node should have a SystemError class. With typescript becoming more and more popular the lack of types is becoming more troublesome. |
I'm going to re-open, but I suspect this will get closed pretty quickly. I could certainly be wrong about that, though. I think there will be a lot of resistance among Node.js core devs to add this stuff and support it. But let's see.... @mscdex @benjamingr @cjihrig You're all still -1 on adding something like |
I'm still -1, but it seems like that ship sailed long ago. Also, https://nodejs.org/api/errors.html#errors_system_errors seems to be what you're looking for, isn't it? |
Yes, still -1 |
For better or for worse,
Alas, this shows try { fs.openSync('fhqwhgads'); } catch (e) { console.log(e.constructor.name); } (Insert sad trombone sound.) Changing it to |
all our errors have well known codes now. I'm not sure why we would need more beyond that |
@devsnek I think @BridgeAR makes some good points in #20803 (comment). I’m not saying there should be subclasses or anything, but I do agree with him in that it would be nice to classify errors in some way (even just programming errors vs. runtime errors would be worth a lot, imo). |
if js had a way of matching the catch like java I would absolutely agree but since it doesn't I don't really think it's worth it, especially since the solution wouldn't be standard. (compare to dom exception enum or whatever) |
Btw, lots of errors from our C++ code still don’t have codes. :) |
@cjihrig @mscdex mind explaining why the -1 for just SystemError? @cjihrig https://nodejs.org/api/errors.html#errors_system_errors is documentation, not a type. I shouldn't have to refer to online documentation when modern IDE's offer intellisense. It took me a while to find that link to see attributes that would have been just a "." away with intellisense. (thanks for the link though)
@devsnek Better intellisense = faster and less error prone node development experience for users. I can see all the attributes and the documentation and type for each attribute if a object is properly typed. And if I do something stupid like adding 1 to SystemError.code (a string) a type system like typescript would warn me so I can fix my mistake. |
@Almenon can you explain how a |
@devsnek it doesn't break intellisense, I'm talking about the lack of intellisense for systemerror. For example, take the following member of childprocess: on(event: "error", listener: (err: Error) => void): this; It is typed as Error, but it should be typed as SystemError. SystemError has several extra properties not present in Error. |
isn't that a problem with your intellisese types, not node? |
I don't own node's intellisense types - node's types are defined by the node team over at https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node I'm not sure if the types are automatically generated or manually defined, maybe the change I want just needs to happen over in DefinitelyTyped. I'll open a issue there. |
Having a proper error type hierarchy does have the advantage of identifying errors by category with Just because everything can be one type, doesn't mean it should be. 🙄 |
That’s a disadvantage since Any types node adds should offer a robust cross-realm brand checking mechanism. |
Potentially OT, but I'd like a way to create synthetic system errors,
especially common FS errors like ENOENT, from Node's API. All I really need
is the constructor part, not the subclassing part.
(Please point me to the right issue if this isn't the correct place to
request this.)
…On Tue, Feb 19, 2019 at 15:37 Jordan Harband ***@***.***> wrote:
That’s a disadvantage since instanceof is both unreliable and doesn’t
work cross-realm.
Any types node adds should offer a robust cross-realm brand checking
mechanism.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#8342 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AERrBOfCyt71DQdcqIFj4FdwW_3Q0fzSks5vPGCagaJpZM4JwyAA>
.
|
@ljharb what about |
@Almenon that’s robust, but can’t be used to differentiate between non-function objects. |
Do you think there would be any chance to add a new language feature that could replace
I agree about that and to do so we could use another property similar to the That is not ideal either but it's likely the best we can do at the moment. |
@BridgeAR I would dearly love one, but https://github.com/jasnell/proposal-istypes was the last attempt. A |
@Ginden that particular one isn't helpful for me since it won't work in non-node environments; but yes, a |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment. For more information on how the project manages feature requests, please consult the feature request management document. |
As we know, ECMAScript define only few
Error
subclasses. W3C specifications adds to this listDOMException
andURIError
. Right now, Node.js implements sevenErrors
:On other side we have Java standard library that defines 74 classes extending java.lang.Exception and numerous other extending these subclasses. Most of them doesn't make sense in Node.js context, as our standard library is much smaller, but I'm convinced that 7 errors defined in ECMAScript are too general to provide meaningful information on types of error that can happen.
Therefore, I propose:
SystemError
(class SystemError extends Error
)FileSystemError
andNetworkError
to inherit fromSystemError
DeprecationError
inheriting fromErrror
for deprecated featuresError
in standard libraryDisadvantages:
err.constructor === TypeError
can breakAdvantages:
.catch(klass, handler)
easier to useLoose ideas:
DatabaseError
to be subclassed by database modulesI'm working on pull request to replace
new Error
withnew TypeError
ornew RangeError
wherever it makes sense.The text was updated successfully, but these errors were encountered: