Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle template errors (and other errors) in angular 2?

When ever there is a template error in angular 2, the entire application fails to work.

Shouldn't only the component that had the template that caused the error, fail to work and the rest of the application be working fine?

How to handle errors so that the application won't stop being responsive when an error occurs?

like image 323
harryjohn Avatar asked Mar 27 '17 07:03

harryjohn


People also ask

How does Angular handle multiple errors?

One traditional way of handling errors in Angular is to provide an ErrorHandler class. This class can be extended to create your own global error handler. This is also a useful way to handle all errors that occur, but is mostly useful for tracking error logs.

How does Angular handle response errors?

The basic way to handle errors in Angular is to use Angular's HttpClient service along with RxJS operators throwError and catchError. The HTTP request is made, and it returns the data with a response if anything wrong happens then it returns an error object with an error status code.

What are two forms of error handling?

SH, structured error handling in Windows PowerShell does in fact work. The reason that sometimes it may appear to not work is because of the difference in the types of errors. Essentially, there are two types of errors: terminating and non-terminating.

What is the error component in angular 2?

The ErrorHandler class in Angular 2+ provides a hook for centralized exception handling. The default implementation of ErrorHandler prints error messages to the console. This service is very simple by design. To intercept the error handling we need to write a custom handler.


1 Answers

You can use custom ErrorHandler:

class MyErrorHandler implements ErrorHandler {
  handleError(error) {
    // do something with the exception
  }
}
@NgModule({
  providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
})
class MyModule {}
like image 54
Sasxa Avatar answered Sep 28 '22 23:09

Sasxa