Skip to content

Instantly share code, notes, and snippets.

@gunar
Created November 1, 2016 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gunar/cc4b65d752287d92111303e9bc6472c6 to your computer and use it in GitHub Desktop.
Save gunar/cc4b65d752287d92111303e9bc6472c6 to your computer and use it in GitHub Desktop.
How to differentiate exceptions from rejections
// 1. Using rejections for both exceptions and rejections
foo(5)
.then(bar)
.catch(err => {
if (!(err instanceof MyError)) {{
// exception!
throw err
}
// else, process business logic error properly
})
// 2. Using data.Task
// Exceptions will throw and task will never reject nor resolve
foo(5)
.fork(err => {
// business logic error
}, bar)
// 3. Returning errors inside a resolve
foo(5)
.then(bar => {
if (bar instanceof MyError) {
// handle business logic error
return
}
// ...
})
.catch(err => {
// handle exception
})
@gunar
Copy link
Author

gunar commented Nov 1, 2016

Gleb Bahmutov Mod Gunar C. Gessner • 2 hours ago

I rarely try to handle specific errors by looking at the error class or information.
Instead I prefer to deal with just the fact that something has not happened.
For example, if I wanted to create a file and there was an error, I might print it, but not try to
look at the reason ("out of space" - nothing I can do, "busy" - retry again)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment