7

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?

2
  • 3
    You are suppose to fix them and not ship code with errors (: It's a good thing that app fails. You can, however, use custom ErrorHandler... Commented Mar 27, 2017 at 7:54
  • 1
    Facing the same issue but seems no fix around template parsing error. @harryjohn did you get any fix around this ?? Commented Jul 16, 2017 at 8:59

2 Answers 2

5

You can use custom ErrorHandler:

class MyErrorHandler implements ErrorHandler {
  handleError(error) {
    // do something with the exception
  }
}
@NgModule({
  providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
})
class MyModule {}
Sign up to request clarification or add additional context in comments.

8 Comments

Great answer, But when this class calls ? on load of app ? or on some particular component ?, should i called this in main module ? , please provide bit more explanation if you can. thanks
@PardeepJain It's a root provider, so it should work for the whole app. I don't know what happens if you have more then one ErrorHandler with lazy loaded modules and their injector. I wrote just two of them: one for a real app that saves error to firebase; second to stop those annoying zone exceptions when serving with HMR and do styling work. But both are just like this example, with provider at AppModule level. I didn't explore it further...
okay thank alot for info, one thing more i tried this but throw some error dont know whats the reason is, should i have to create seprate module for ErrorHandler ? or just import within appModule like you did in answer ?
@PardeepJain No need for separate module, unless you need a module (: I put it in CoreModule in my app, but it shouldn't matter, as long as it's provided at root level (handled by root injector/provided in eagerly loaded module). Not sure if I'm clear, but as long as you don't do this, you're good (:
@Sasxa Consider a pipe that can sometimes throw an error for wrong inputs that's passed to it. These errors can occur at runtime and won't be caught during development. My questions is related to this. Is there a way to handle such errors, so that everything, except the component that threw the error, will work as expected?
|
2

Errors that comes while parsing the angular template or while evaluating the expression cannot be handled by angular Errorrhandller. These kind of errors needs to be fixed.

You can avoid the following mistakes to prevent the template error occurring: Don't bind methods in directives like *ngIf , *ngFor. Don't write big expression in directives which likely would cause error. Keep a minimal amount of logic in templates.

Anyone can add if anything missing.

Comments

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.