node icon indicating copy to clipboard operation
node copied to clipboard

errors.js module

Open streamich opened this issue 7 years ago • 9 comments

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.

streamich avatar Jul 31 '17 13:07 streamich

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.

jasnell avatar Jul 31 '17 16:07 jasnell

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.

XGHeaven avatar Jul 31 '17 16:07 XGHeaven

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

streamich avatar Aug 01 '17 10:08 streamich

I've opened an issue (https://github.com/nodejs/node/issues/14216) and a PR (https://github.com/nodejs/node/pull/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 😣 )

starkwang avatar Aug 09 '17 06:08 starkwang

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?

gireeshpunathil avatar Dec 30 '19 09:12 gireeshpunathil

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

marsonya avatar Mar 18 '21 05:03 marsonya

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.

github-actions[bot] avatar Mar 22 '22 21:03 github-actions[bot]

Please keep this open.

sindresorhus avatar Mar 23 '22 07:03 sindresorhus

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.

github-actions[bot] avatar Sep 21 '22 01:09 github-actions[bot]

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.

github-actions[bot] avatar Nov 21 '22 01:11 github-actions[bot]

Please re-open this (?)

titanism avatar Feb 20 '23 19:02 titanism

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

ThePotatoChronicler avatar Feb 27 '23 19:02 ThePotatoChronicler

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

nlhnt avatar Apr 15 '23 16:04 nlhnt

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.

ThePotatoChronicler avatar Apr 16 '23 12:04 ThePotatoChronicler