1

I have this question here on how Angular2+ Observable handle Http errors. When we use Angular2+ Http APIs to make requests, if the response back has error status code, and error will be thrown.

In the Angular2 Official Starter project, the error handler function to be called in:

someObs.catch(handler)

In the handler, it actually returns:

Observable.throw(...)

My question is, wouldn't this thrown new error that could potentially crash the web app? Since in Javascript if you throw some error, the web app could then unexpected behaviours.

My understanding of Observable.catch is that when some error happens, we catch it we do something else(instead of throw a new error). And since the

Observable.catch

still requires us to return an Observable, would it be a best solution to return:

Observable.empty()

as it would not emit any new value so that subscribers won't react

1 Answer 1

1

If you are able to handle a particular error inside catch, you can return an observable that is used to continue the observable chain. Returning Observable.empty() might make sense - depending upon the situation.

If you cannot handle the caught error and you return Observable.empty, you've swallowed the error - which is, generally speaking, not a great idea (especially if the catch handler does not include some sort of reporting mechanism). Instead, re-throwing the error - or returning Observable.throw - gives the subscriber the opportunity to handle or report the error.

If the subscriber does not handle the error, what happens depends on the runtime. In the browser, the application won't crash due to the error being unhandled; it'll just be reported as an unhandled error - much the same as an unhandled promise rejection or an uncaught thrown error. In Node, it will fail fast/crash by default.

Sign up to request clarification or add additional context in comments.

4 Comments

I kinda get it know. It's like the observable sees the error, but not sure how to handle it and so pass it to the subscriber. Or it maybe that the catch clause parse random errors into a agreed-format in the app, and then let the subscriber process it.
But sometimes when I let an observable error to be thrown, the application does produce unexpected behaviours: in my case, the buttons and inputs still work(can still click or enter), but the javascript handlers no longer respond
I always make it a point to handle errors in component alone, so that I know what I can do when things go wrong. In different scenarios we want to do different things
also, you can create a global error handler to handle somehow the errors, which you didn't handle in your services/components. Here's an article on it medium.com/@amcdnl/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.