Skip to content

provideRouter: there is no option for errorHandler #48759

@CSchulz

Description

@CSchulz

Which @angular/* package(s) are relevant/related to the feature request?

router

Description

This is a followup of #48295.

There is no replacement for ExtraOptions with new provideRouter
eg

RouterModule.forRoot(APPLICATION_ROUTES, {
        errorHandler: (error: any) => console.error('Router Error Occured', error)
      }),

The linked issue mentioned you can use the Router.events with NavigationError but this is not usable for all variants of handling navigation errors.
The defaultErrorHandler is throwing the error and can't be overwritten:

function defaultErrorHandler(error: any): any {
throw error;
}

this.restoreHistory(overallTransitionState, true);
const navError = new NavigationError(
overallTransitionState.id,
this.serializeUrl(overallTransitionState.extractedUrl), e,
overallTransitionState.targetSnapshot ?? undefined);
eventsSubject.next(navError);
try {
overallTransitionState.resolve(this.errorHandler(e));
} catch (ee) {
overallTransitionState.reject(ee);
}

For the moment the best way is to use an own provide for the ROUTER_CONFIG injection token.

Proposed solution

Add new settings to Router config

 provideRouter(APPLICATION_ROUTES, withRouterConfig({
        errorHandler: (error: any) => console.error('Router Error Occured', error)
    })),

Alternatives considered

There is non if you want to fluent API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions