Sub topic 13
Handling resolver failures
This chapter assumes you can write a basic ResolveFn, read route data in a component, and use RxJS operators such as catchError. It covers what happens when a resolver fails and how to recover.
What happens when a resolver fails
The router waits for every resolver on a route before it activates the route. If a resolver's Observable errors, the navigation is cancelled with an error. The user stays on the current page, and nothing on screen explains why. The router emits a NavigationError event. If nobody handles it, the error goes to the router's error handler.
A second failure is easier to miss. If an Observable completes without emitting a value, the navigation is cancelled and the route never activates. An empty result from a ResolveFn looks like a hang. Resolver Observables must emit at least once.
Redirecting with RedirectCommand
From Angular 18, a resolver can return a RedirectCommand. The router cancels the current navigation and redirects to the UrlTree inside the command. This works like returning a UrlTree from a guard.
import { inject } from '@angular/core';
import { RedirectCommand, ResolveFn, Router } from '@angular/router';
import { catchError, of } from 'rxjs';
import { Product, ProductService } from './product.service';
export const safeProductResolver: ResolveFn<Product | RedirectCommand> = (route) => {
const router = inject(Router);
return inject(ProductService).getById(route.paramMap.get('id')!).pipe(
catchError(() => of(new RedirectCommand(router.parseUrl('/not-found')))),
);
};Two details matter here:
catchErrormust return an Observable, so the command is wrapped inof(...).- The return type is
Product | RedirectCommand. Your component never receives aRedirectCommand, because the router consumes it.
Choosing a recovery strategy
Not every failure deserves a redirect. Pick the response that matches the cause.
| Failure | Reasonable response |
|---|---|
| Item does not exist (404) | Redirect to a not-found page |
| User lacks access (403) | Redirect to an error or login page |
| Network error or server error | Redirect to an error page, or retry first |
| Data is optional | Return a fallback value such as `null` and let the component handle it |
You can combine strategies. Retry transient errors before you give up:
import { retry, catchError, of } from 'rxjs';
// inside the resolver
return inject(ProductService).getById(id).pipe(
retry({ count: 2, delay: 300 }),
catchError(() => of(new RedirectCommand(router.parseUrl('/not-found')))),
);Handling the error globally
For failures you did not anticipate, register a handler with withNavigationErrorHandler in provideRouter. It receives the error and can log it or redirect.
import { inject } from '@angular/core';
import { provideRouter, withNavigationErrorHandler, RedirectCommand, Router } from '@angular/router';
import { routes } from './app.routes';
export const appProviders = [
provideRouter(
routes,
withNavigationErrorHandler((error) => {
console.error('Navigation failed', error.error);
return new RedirectCommand(inject(Router).parseUrl('/error'));
}),
),
];Returning a RedirectCommand from this handler is supported from Angular 19. In earlier versions, handle errors inside each resolver.
Exercise
A resolver calls getById(id) and uses catchError(() => EMPTY) to hide failures. Users report that clicking a product link does nothing. Why does this happen, and how would you fix it?
Show answer
EMPTY completes without emitting, so the router cancels the navigation silently and the user stays on the current page. Replace it with of(new RedirectCommand(router.parseUrl('/not-found'))), or with of(null) if the component can handle missing data, so the resolver always emits a value.