Skip to content

errors.js module #14554

Description

@streamich

Node now has internal/errors.js module which allows to set static error codes for errors, which is great. Would be good to expose Node's internal error constructors in a new module say require('errors').

import {TypeError as NodesTypeError} from 'errors';

My use case is as follows: I am now rewriting an in-memory file system module memfs that mimics how fs module works, I would like to throw exactly the same errors as Node does, so that it can be used for testing, so now I have to basically copy-paste the internal/errors.js file into my project to be able to create similar errors to what Node does.

Activity

  1. added
    feature requestIssues requesting new Node.js features.
    errorsIssues and PRs related to JavaScript errors originating in Node.js core.
    on Jul 31, 2017
  2. jasnell commented on Jul 31, 2017

    @jasnell
    Member

    Eventually this may be a possibility. For the time being, however, while we are still in the process of migrating all of our internal errors, I would rather avoid it. The reason is due to the fact that changes to that internal API are still possible / likely as we move along.

  3. XGHeaven commented on Jul 31, 2017

    @XGHeaven
    Contributor

    I think we can create a new module named errors(better) or others.
    errors export Error/TypeError/RangeError/... just like internal/errors exports.
    And more, errors export a helper function to create ErrorClass with some type.
    We assign a temp name makeError to helper function.

    const PathNotFoundError = errors.makeError('ERR_PATH_NOT_FOUND', '%s not found')
    const err = new PathNotFoundError('/usr/foo/bar')
    // err.code === 'ERR_PATH_NOT_FOUND'
    // err.message === '/usr/foo/bar not found'

    By the way, we can export all errors code used in internal/errors to errors. Just like errors.ERR_ASSERTION that equals 'ERR_ASSERTION'.
    This can help user to check which error and don't need to remember code string.
    String cannot autocompletion with IDE like WS, but exports variable can do.

  4. streamich commented on Aug 1, 2017

    @streamich
    Author

    @jasnell Maybe we can export just the constructors like the internal TypeError and mark this API as experimental.

  5. starkwang commented on Aug 9, 2017

    @starkwang
    Contributor

    I've opened an issue (#14216) and a PR (#14250) earlier before about separating the error codes to a new module, but the errors are still being migrated so they are blocked temporarily. IMHO, the appropriate time to do these is after the migration are done.
    (But the migration is slower than I expected 😣 )

  6. gireeshpunathil commented on Dec 30, 2019

    @gireeshpunathil
    Member

    just trying to understand what needs to be done here:

    • make internal/errors.js external
    • make sure all the error classes are documented, if not already

    is that so?

  7. marsonya commented on Mar 18, 2021

    @marsonya
    Member

    There's been no activity on this issue for a long time. @nodejs/documentation, any update on this?

  8. github-actions commented on Mar 22, 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.

  9. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Mar 22, 2022
  10. moved this from Pending Triage to Stale in Node.js feature requestson Mar 22, 2022
  11. moved this to Pending Triage in Node.js feature requestson Mar 22, 2022
  12. sindresorhus commented on Mar 23, 2022

    @sindresorhus

    Please keep this open.

  13. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Mar 24, 2022
  14. github-actions commented on Sep 21, 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.

  15. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Sep 21, 2022
  16. ghost deleted a comment from on Oct 21, 2022
  17. github-actions commented on Nov 21, 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.

  18. titanism commented on Feb 20, 2023

    @titanism

    Please re-open this (?)

  19. ThePotatoChronicler commented on Feb 27, 2023

    @ThePotatoChronicler

    This is such a useful feature, yet it's dead again

  20. nlhnt commented on Apr 15, 2023

    @nlhnt

    Jesus... I want to test for the type of error in my unit tests and it seems like a headache? What

  21. ThePotatoChronicler commented on Apr 16, 2023

    @ThePotatoChronicler

    Jesus... I want to test for the type of error in my unit tests and it seems like a headache? What

    There is just no good way as far as I've looked. The only way I have ever found is to check if it's an instanceof Error and then check the .name property. It's horrid. I've ever only found pain when dealing with JS exceptions.

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