Error Handling, Retry and Loading States

Angular 14 min min read Updated: Nov 16, 2026 Intermediate
Error Handling, Retry and Loading States
Intermediate Topic 3 of 4

Things That Go Wrong

StatusMeaningWhat to show
0No network or CORSCheck connection
400Bad request or validationShow field errors
401Not logged inGo to login
403Not allowedPermission message
404Not foundNot found page
500+Server problemTry again later

catchError in a Service

typescript getUsers(): Observable<User[]> { return this.http.get<User[]>('/api/users').pipe( catchError((err: HttpErrorResponse) => { console.error(err); return throwError(() => new Error(this.messageFor(err))); // friendly message })); } private messageFor(err: HttpErrorResponse): string { if (err.status === 0) return 'Cannot reach the server.'; if (err.status === 404) return 'We could not find that.'; if (err.status >= 500) return 'Server error. Please try again.'; return err.error?.message ?? 'Something went wrong.'; }

Retry

typescript import { retry, timer } from 'rxjs'; this.http.get<User[]>('/api/users').pipe( retry({ count: 3, delay: (error, attempt) => { if (error.status >= 400 && error.status < 500) throw error; // do not retry client errors return timer(500 * 2 ** (attempt - 1)); // 500ms, 1s, 2s (backoff) }, }));
Only retry safe requests: Retrying GET is fine. Retrying POST can create duplicates unless your API is idempotent.

A Loading / Error / Data State with Signals

typescript export class UsersPage { private service = inject(UserService); users = signal<User[]>([]); loading = signal(false); error = signal<string | null>(null); constructor() { this.load(); } load() { this.loading.set(true); this.error.set(null); this.service.getUsers().subscribe({ next: list => { this.users.set(list); this.loading.set(false); }, error: err => { this.error.set(err.message); this.loading.set(false); }, }); } }
html @if (loading()) { <div class="spinner" role="status">Loading...</div> } @else if (error()) { <div class="alert"> <p>{{ error() }}</p> <button (click)="load()">Try again</button> </div> } @else if (users().length === 0) { <p>No users yet. Add the first one!</p> } @else { <ul> @for (u of users(); track u.id) { <li>{{ u.name }}</li> } </ul> }

Reusable Helper

typescript export type Loadable<T> = | { status: 'loading' } | { status: 'error'; message: string } | { status: 'ready'; data: T }; export function toLoadable<T>() { return (source: Observable<T>): Observable<Loadable<T>> => source.pipe( map(data => ({ status: 'ready', data }) as Loadable<T>), startWith({ status: 'loading' } as Loadable<T>), catchError(err => of({ status: 'error', message: err.message } as Loadable<T>))); } // usage vm = toSignal(this.service.getUsers().pipe(toLoadable()));
html @switch (vm()?.status) { @case ('loading') { <p>Loading...</p> } @case ('error') { <p>Failed to load.</p> } @case ('ready') { <!-- use vm().data --> } }

Handling Server Validation Errors

typescript save() { this.service.create(this.form.getRawValue()).subscribe({ next: () => this.router.navigate(['/users']), error: (err: HttpErrorResponse) => { if (err.status === 400 && err.error?.errors) { Object.entries(err.error.errors).forEach(([field, message]) => this.form.get(field)?.setErrors({ server: message })); } }, }); }
Optimistic updates: For quick actions such as a like button, update the screen first, send the request, and roll back if it fails. It feels instant.

Conclusion

Always plan for loading, error and empty states. Use catchError for friendly messages, retry with backoff for temporary failures and a small state pattern to keep templates clean.

Quick Summary: loading/error/data signals + catchError + retry({count, delay}).

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators