Skip to content

Should Node.js standard library throw custom Error subclasses? #8342

Description

@Ginden

As we know, ECMAScript define only few Error subclasses. W3C specifications adds to this list DOMException and URIError. Right now, Node.js implements seven Errors:

  • Error
  • EvalError
  • RangeError
  • ReferenceError
  • SyntaxError
  • TypeError
  • URIError

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:

  • make all exceptions coming from syscalls inherit from new class SystemError (class SystemError extends Error)
    • create classes like FileSystemError and NetworkError to inherit from SystemError
  • add class DeprecationError inheriting from Errror for deprecated features
  • discourage creating direct instances of Error in standard library

Disadvantages:

  • code directly checking err.constructor === TypeError can break

Advantages:

  • better typing for TypeScript
    • many IDEs can use TypeScript definition files and use them as ad-hoc documentation, even if you write in JavaScript
  • Bluebird .catch(klass, handler) easier to use

Loose ideas:

  • export classes like DatabaseError to be subclassed by database modules

I'm working on pull request to replace new Error with new TypeError or new RangeError wherever it makes sense.

Activity

  1. mscdex commented on Aug 30, 2016

    @mscdex
    Contributor

    A couple of immediate thoughts:

    • Node.js does not implement those *Error objects, V8 does
    • -1 to exporting random non-core-related Error objects like DatabaseError, IMHO that is out of scope for node
  2. Qard commented on Aug 30, 2016

    @Qard
    Member

    DatabaseError probably does not makes sense. Most database errors would be NetworkError. There may be query-related errors, but those should probably be more specific to the particular database module, so I'm also -1 on that.

    The other stuff sounds good to me though. Being able to type-check errors would be fantastic.

  3. jasnell commented on Aug 30, 2016

    @jasnell
    Member

    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.

  4. Qard commented on Aug 30, 2016

    @Qard
    Member

    @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. 😸

  5. cjihrig commented on Aug 30, 2016

    @cjihrig
    Contributor

    -1 to defining new error types.

  6. benjamingr commented on Sep 4, 2016

    @benjamingr
    Member

    I just want to point out you can use stuff like .catch in bluebird without error subclasses - by using something like https://github.com/petkaantonov/core-error-predicates .

    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).

  7. dead-claudia commented on Oct 3, 2016

    @dead-claudia

    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 FSError, NetworkError, CryptoError, etc. (just the high level) would suffice.

  8. added
    errorsIssues and PRs related to JavaScript errors originating in Node.js core.
    and removed on Oct 28, 2016
  9. Trott commented on Jul 15, 2017

    @Trott
    Member

    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.

  10. Almenon commented on Feb 18, 2019

    @Almenon

    @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.

  11. Trott commented on Feb 18, 2019

    @Trott
    Member

    @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 SystemError?

  12. 14 remaining items

  13. ljharb commented on Feb 19, 2019

    @ljharb
    SponsorMember

    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.

  14. dead-claudia commented on Feb 19, 2019

    @dead-claudia
  15. Almenon commented on Feb 20, 2019

    @Almenon

    @ljharb what about typeof?

  16. ljharb commented on Feb 20, 2019

    @ljharb
    SponsorMember

    @Almenon that’s robust, but can’t be used to differentiate between non-function objects.

  17. BridgeAR commented on Feb 20, 2019

    @BridgeAR
    Member

    @ljharb

    That’s a disadvantage since instanceof is both unreliable and doesn’t work cross-realm.

    Do you think there would be any chance to add a new language feature that could replace instanceof in favor of one which works cross-realm?

    Any types node adds should offer a robust cross-realm brand checking mechanism.

    I agree about that and to do so we could use another property similar to the code property which groups different Node.js errors together.

    That is not ideal either but it's likely the best we can do at the moment.

  18. ljharb commented on Feb 20, 2019

    @ljharb
    SponsorMember

    @BridgeAR I would dearly love one, but https://github.com/jasnell/proposal-istypes was the last attempt.

    A .code property isn't sufficient; it'd need to be like Array.isArray - a static brand-checking method.

  19. Ginden commented on Feb 20, 2019

    @Ginden
    Author
  20. ljharb commented on Feb 20, 2019

    @ljharb
    SponsorMember

    @Ginden that particular one isn't helpful for me since it won't work in non-node environments; but yes, a util.types method for node-specific errors would certainly suffice.

  21. github-actions commented on Feb 28, 2022

    @github-actions
    Contributor

    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.

  22. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Feb 28, 2022
  23. github-actions commented on Apr 4, 2022

    @github-actions
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    errorsIssues and PRs related to JavaScript errors originating in Node.js core.feature requestIssues requesting new Node.js features.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions