Handling Application Errors

Learn how to handle runtime errors in an Angular application.

The most usual runtime errors in an Angular application come from the interaction with an HTTP API. Entering the wrong login credentials or sending data in the wrong format can result in an HTTP error. An Angular application can handle HTTP errors in the following ways:

  • Explicitly during the execution of a particular HTTP request

  • Globally in the global error handler of the application

  • Centrally using an HTTP interceptor

In the following section, we will explore how to handle an HTTP error in a specific HTTP request.

Catching HTTP request errors

Handling errors in HTTP requests typically requires manually inspecting the information returned in the error response object. RxJS provides the catchError operator to simplify that. In conjunction with the pipe operator, it can catch potential errors that may occur when initiating an HTTP request:

Get hands-on with 1200+ tech skills courses.