[ Web Proxy ]
URL:
Viewing: https://angular.dev/api/router/withNavigationErrorHandler [Back]  [Original]

withNavigationErrorHandler Angular Skip to main content
menu
close
more_horiz
menuAPI
@angular/router

withNavigationErrorHandler

function
stable

Provides a function which is called when a navigation error occurs.

NavigationErrorinjectrunInInjectionContextCentralize error handling in withNavigationErrorHandler

On this page

    arrow_upward_alt Back to the top

    API

    @paramhandler(error: NavigationError) => unknown
    @returnsNavigationErrorHandlerFeature

    A set of providers for use with provideRouter.

    Description

    Provides a function which is called when a navigation error occurs.

    This function is run inside application's injection context so you can use the inject function.

    This function can return a RedirectCommand to convert the error to a redirect, similar to returning a UrlTree or RedirectCommand from a guard. This will also prevent the Router from emitting NavigationError; it will instead emit NavigationCancel with code NavigationCancellationCode.Redirect. Return values other than RedirectCommand are ignored and do not change any behavior with respect to how the Router handles the error.

    Usage Notes

    Basic example of how you can use the error handler option:

    const appRoutes: Routes = [];
    bootstrapApplication(AppComponent,
      {
        providers: [
          provideRouter(appRoutes, withNavigationErrorHandler((e: NavigationError) =>
    inject(MyErrorTracker).trackError(e)))
        ]
      }
    );
    Jump to details

    Web Proxy Viewer  |  New URL  |  Original Page