Sub topic 5
Showing error state to users
This chapter assumes you can write an Angular component with signals and that you know the UserService from the previous chapter. Its getUser method returns an Observable<User> that fails with an Error carrying a readable message. Here you will show that message in the UI.
Why a failed request needs its own state
A request that fails leaves the screen with nothing to show. If the component only stores the data, the user sees a blank area and cannot tell whether the data is still loading, empty, or broken. Model every request with three outcomes: loading, success and error. Each outcome gets its own part of the template.
Keeping error state in the component
The service already turned an HttpErrorResponse into an Error with a message. The component's job is to store that message and render it. The example below uses three signals and a load method that resets the state each time it runs.
import { Component, OnInit, inject, signal } from '@angular/core';
import { User, UserService } from './user.service';
@Component({
selector: 'app-user-card',
standalone: true,
template: `
@if (loading()) {
<p>Loading...</p>
} @else if (errorMessage()) {
<p role="alert">{{ errorMessage() }}</p>
<button type="button" (click)="load()">Try again</button>
} @else if (user(); as u) {
<h2>{{ u.name }}</h2>
}
`,
})
export class UserCardComponent implements OnInit {
private userService = inject(UserService);
protected user = signal<User | null>(null);
protected loading = signal(false);
protected errorMessage = signal<string | null>(null);
ngOnInit(): void {
this.load();
}
protected load(): void {
this.loading.set(true);
this.errorMessage.set(null);
this.userService.getUser(1).subscribe({
next: (u) => {
this.user.set(u);
this.loading.set(false);
},
error: (e: Error) => {
this.errorMessage.set(e.message);
this.loading.set(false);
},
});
}
}Three details matter here:
loadclearserrorMessagebefore it starts. Without that reset, an old error stays on screen during the retry.loadingis set back tofalsein both callbacks. Forgetting it in theerrorcallback leaves a spinner running forever.- The "Try again" button calls the same
loadmethod, so the first attempt and a manual retry follow one code path.
Writing messages users can act on
The text in errorMessage is for people, not for developers. Keep the status code and the raw server body out of it. Map each category from earlier in this tutorial to advice the user can follow:
| Category | Message for the user | Offer a retry button? |
|---|---|---|
| Network error (status 0) | Check your connection and try again. | Yes |
| 404 | We could not find that item. | No |
| Other 4xx | The request was not accepted. | Usually no |
| 5xx | Something went wrong on our side. | Yes |
Log the technical detail separately, for example with the global ErrorHandler covered later. The screen carries the plain message and the log carries the evidence.
Accessibility and stale data
Put role="alert" on the message element, as the example does. Screen readers announce an alert as soon as it appears, so users who cannot see the screen still learn that the request failed.
Also decide what happens to data you already have. If a refresh fails after an earlier success, you can keep the old user visible and show the error beside it, or clear it. Keeping stale data is often kinder, but label it so users do not trust it as current.
Exercise
In the component above, load sets errorMessage to null before calling the service. What visible problem appears if that line is removed and the user clicks "Try again" while the server is still down?
Show answer
The old error message stays visible while the new request is in flight. Because loading is true the template shows "Loading..." first, but if the retry fails the message is replaced only by the same text, so the user cannot tell a new attempt happened. If the retry succeeds, the stale error is still set, so after loading finishes the template shows the error branch instead of the user. Resetting errorMessage at the start of each attempt prevents both problems.