When we are developing our app, we are in control, and we can see the errors and handle them appropriately.

But this is not the case in production, even if you have tested your app.

In production, you can’t see what your user is seeing and experiencing and you not capable of knowing about errors unless you find a way to be notified about the exceptions.

Angular 2 has his own way to catch and handle errors. The default implementation of ErrorHandler prints errors messages to the console. You can intercept the default error handling and write a custom exception handler that replaces this default as appropriate for your app.

In our case, in production, we want to send the errors to our server. Let’s see how we can do it.

We need to create our own ErrorHandler class that implements the default ErrorHandler. The only method that required is the handleError method that takes the error as an argument.

Next, we need to tell our injector to use our implementation instead of the default.

That’s all. If you don’t want to ignore the Angular default handler, you can extend the ErrorHandler class and also call the default handler like this:

You can play with the code here.

☞ Please tap or click “︎❤” to help to promote this piece to others.