question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Catching errors based on Error.name

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
benjamingrcommented, Sep 29, 2015

Bluebird 3.0 supports something similar. Assuming you don’t want to alias and import the errors directly anyway:

  promise.catch({name: "MyFirstError"}, err => { ... }); // valid Bluebird 3

Thanks for the suggestion. https://github.com/petkaantonov/bluebird/issues/499

0reactions
perrin4869commented, Oct 1, 2015

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:

client.emitAsync('login', credentials)
.catch(err => err.name === 'RetryLoginError', err => { ... })

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found