How to Handle Errors in a Reactive Angular App

Share on facebook
Share on google
Share on twitter
Share on linkedin

In this post, we will cover how to handle errors in a reactive Angular app. To provide a good user experience you should always let your users know what state your application is in. That includes showing a loading spinner when it’s loading and showing error messages if there are any errors.

It is a common pattern that a page consists of the main content alongside a loading screen and some error messages in case of errors.
We will cover a helpful pattern you can follow for creating Angular components that follow this way of working.

The Pattern

Generally, the pattern works like this code:

<ng-container *ngIf="(isLoading$ | async) === false; else loading">
    <ng-container *ngIf="!(error$ | async); error">
        <!-- Content -->
    </ng-container>

    <ng-template #error>
        <ng-container [ngSwitch]="error$ | async">
            <ng-container *ngSwitchCase="someError1">
                <!-- Error message -->
            </ng-container>
            <ng-container *ngSwitchCase="someError2">
                <!-- Error message -->
            </ng-container>
    </ng-template>
</ng-container>

<ng-template #loading>
    <!-- Loading indicator -->
</ng-template>

We first check if we are loading and show a loading spinner in case. Then we show the content if there are no errors or use a ngSwitch to show the error corresponding to that error.

The error$ can be an error id derived from a selector or set in your state when an error has happened eg. “Couldn’t load data. Try again”.

Sometimes, you want to show the content and errors simultaneously and that would look like this:

<ng-container *ngIf="(isLoading$ | async) === false; else loading">
    <ng-container *ngIf="!(error$ | async)">
        <!-- Content -->
    </ng-container>

    <ng-container *ngIf="(error$ | async) as error">
        <ng-container [ngSwitch]="error">
            <ng-container *ngSwitchCase="someError1">
                <!-- Error message -->
            </ng-container>
            <ng-container *ngSwitchCase="someError2">
                <!-- Error message -->
            </ng-container>
    </ng-container>
</ng-container>

<ng-template #loading>
    <!-- Loading indicator -->
</ng-template>

Here we simply allow the content and error to be shown simultaneously if there are any errors.

With Signals

It’s even simpler with Signals and the $ + async just becomes calling the signals getter/().

<ng-container *ngIf="isLoading() === false; else loading">
    <ng-container *ngIf="!error(); error">
        <!-- Content -->
    </ng-container>

    <ng-template #error>
        <ng-container [ngSwitch]="error()">
            <ng-container *ngSwitchCase="someError1">
                <!-- Error message -->
            </ng-container>
            <ng-container *ngSwitchCase="someError2">
                <!-- Error message -->
            </ng-container>
    </ng-template>
</ng-container>

<ng-template #loading>
    <!-- Loading indicator -->
</ng-template>

Conclusion

We learned a pattern to architect your pages so you show spinner, content, and error messages in a reactive way.

Do you want to become an Angular architect? Check out Angular Architect Accelerator.

Related Posts and Comments

How to Handle Errors in a Reactive Angular App

In this post, we will cover how to handle errors in a reactive Angular app. To provide a good user experience you should always let your users know what state your application is in. That includes showing a loading spinner when it’s loading and showing error messages if there are any errors. It is a

Read More »

How to Set Up Git Hooks in an Nx Repo

Git hooks can be used to automate tasks in your development workflow. The earlier a bug is discovered, the cheaper it is to fix (and the less impact it has). Therefore it can be helpful to run tasks such as linting, formatting, and tests when you are e.g. committing and pushing your code, so any

Read More »

The Stages of an Angular Architecture with Nx

Long gone are the times when the frontend was just a dumb static website. Frontend apps have gotten increasingly complex since the rise of single-page application frameworks like Angular. It comes with the price of increased complexity and the ever-changing frontend landscape requires you to have an architecture that allows you to scale and adapt

Read More »

The Best Way to Use Signals in Angular Apps

Since Angular 16, Angular now has experimental support for signals and there is a lot of confusion in the community about whether this is going to replace RxJS or how it should be used in an app in combination with RxJS. This blog post sheds some light on what I think is the best way

Read More »

High ROI Testing with Cypress Component Testing

Testing is one of the most struggled topics in Angular development and many developers are either giving up testing altogether or applying inefficient testing practices consuming all their precious time while giving few results in return. This blog post will change all this as we will cover how I overcame these struggles the hard way

Read More »