Photo by Nathan Dumlao on Unsplash

Expecting the Unexpected — Best practices for Error handling in Angular

Centralize error handling, track errors, and notify about them

Michael Karén
Angular In Depth
Published in
7 min readJan 29, 2019

--

In this article, I write about centralizing Angular error handling. I discuss some of the more common topics such as:

  • client-side errors
  • server-side errors
  • user notification
  • tracking errors

I present some code snippets during the way and lastly provide a link to the full example.

Spanish version:

Whom should we blame for errors?

Why do we have errors in our applications? Why can’t we write code from specifications that always works?

Ultimately, human beings create software, and we are prone to make mistakes. Some reasons behind errors could be:

  1. Complexity of application
  2. Communication between stakeholders
  3. Developer mistakes
  4. Time pressure
  5. Lack of testing

This list could go on and on. With this in mind, the time comes when the unexpected happens, and an error is thrown.

https://xkcd.com/1024/

Catch them if you can

To catch synchronous exceptions in the code, we can add a try/catch block. If an error is thrown inside try then we catch it and handle it. If we don’t do this, the script execution stops.

Understandably, this becomes unsustainable very fast. We can’t try to catch errors everywhere in the code. We need global error handling.

Catch’em all

--

--

Michael Karén
Angular In Depth

Frontend Architect • JavaScript Enthusiast • Educative.io Author • ngVikings organizer.