Catching errors based on Error.name
See original GitHub issueI was thinking that in many cases, it might be more convenient to use a String to catch errors rather than constructors, and the reason is that when it comes to modules, you actually need to export modules, making it much more cumbersome to type. Compare the following:
Promise.resolve(foo)
.then(() => { ... })
.catch(MyModule.errors.MyFirstError, err => { ... })
.catch(MyModule.errors.MySecondError, err => { ... })
...
vs:
Promise.resolve(foo)
.then(() => { ... })
.catch("MyFirstError", err => { ... })
.catch("MySecondError", err => { ... })
...
Where in the first catch err.name === "MyFirstError"
and in the second catch err.name === "MySecondError"
.
Issue Analytics
- State:
- Created 8 years ago
- Comments:10
Top Results From Across the Web
Error handling, "try...catch" - The Modern JavaScript Tutorial
So, try...catch can only handle errors that occur in valid code. Such errors are called “runtime errors” or, sometimes, “exceptions”.
Read more >Control flow and error handling - JavaScript - MDN Web Docs
The try...catch statement marks a block of statements to try, and specifies one or more responses should an exception be thrown. If an...
Read more >8. Errors and Exceptions — Python 3.11.1 documentation
Most exceptions are defined with names that end in “Error”, similar to the naming of the standard exceptions. Many standard modules define their...
Read more >A mostly complete guide to error handling in JavaScript.
Learn how to deal with errors and exceptions in synchronous and asynchronous JavaScript code.
Read more >Handling specific errors in JavaScript (think exceptions)
Using try-catch-finally. js, you can call the _try function with an anonymous callback, which it will call, and you can chain . catch...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Bluebird 3.0 supports something similar. Assuming you don’t want to alias and import the errors directly anyway:
Thanks for the suggestion. https://github.com/petkaantonov/bluebird/issues/499
Well, the only problem I have with ES6 exports is that unfortunately, they do not yet work natively in node.js, even the newest 4.1.x versions, so they are not a viable solution yet.
OK, my very last pitch to supporting strings in catch clauses will be that there are times when constructors are just not available at all. For example, take socket.io. If, say, the client gives a callback to their emit to the server, and the server throws and error and wants to hand it to the client, the error gets serialized and sent back as json to the client… This is what I mean:
Of course this means that the client can’t have a constructor because the error they got is not an instance of
Error
, and it could never be. At that point, the only way to write multiple catch clauses is to use predicate functions, which work perfectly, but are more verbose than I would like.