You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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{TypeErrorasNodesTypeError}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.
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.
Reacted by Jeremiah Senkpiel and Ruben Bridgewater
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.
constPathNotFoundError=errors.makeError('ERR_PATH_NOT_FOUND','%s not found')consterr=newPathNotFoundError('/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.
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 😣 )
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.
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.
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.
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.
Activity
jasnell commentedon Jul 31, 2017
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.
XGHeaven commentedon Jul 31, 2017
I think we can create a new module named
errors(better) or others.errorsexportError/TypeError/RangeError/...just likeinternal/errorsexports.And more,
errorsexport a helper function to createErrorClasswith some type.We assign a temp name
makeErrorto helper function.By the way, we can export all errors code used in
internal/errorstoerrors. Just likeerrors.ERR_ASSERTIONthat 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.
streamich commentedon Aug 1, 2017
@jasnell Maybe we can export just the constructors like the internal
TypeErrorand mark this API as experimental.starkwang commentedon Aug 9, 2017
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 😣 )
gireeshpunathil commentedon Dec 30, 2019
just trying to understand what needs to be done here:
internal/errors.jsexternalis that so?
marsonya commentedon Mar 18, 2021
There's been no activity on this issue for a long time. @nodejs/documentation, any update on this?
github-actions commentedon Mar 22, 2022
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.
sindresorhus commentedon Mar 23, 2022
Please keep this open.
github-actions commentedon Sep 21, 2022
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 commentedon Nov 21, 2022
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.
titanism commentedon Feb 20, 2023
Please re-open this (?)
ThePotatoChronicler commentedon Feb 27, 2023
This is such a useful feature, yet it's dead again
nlhnt commentedon Apr 15, 2023
Jesus... I want to test for the type of error in my unit tests and it seems like a headache? What
ThePotatoChronicler commentedon Apr 16, 2023
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.