feat(router): add support for custom error handlers

This commit is contained in:
vsavkin
2016-08-25 07:56:30 -07:00
committed by Victor Berchet
parent 93f323cfa2
commit 2fc5c57b31
4 changed files with 53 additions and 2 deletions

View File

@ -201,6 +201,21 @@ export class RoutesRecognized {
export type Event =
NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized;
/**
* Error handler that is invoked when a navigation errors.
*
* If the handler retuns a value, the navigation promise will be resolved with this value.
* If the handler throws an exception, the navigation promise will be rejected with
* the exception.
*
* @stable
*/
export type ErrorHandler = (error: any) => any;
function defaultErrorHandler(error: any): any {
throw error;
}
/**
* The `Router` is responsible for mapping URLs to components.
*
@ -216,6 +231,8 @@ export class Router {
private navigationId: number = 0;
private configLoader: RouterConfigLoader;
errorHandler: ErrorHandler = defaultErrorHandler;
/**
* Indicates if at least one navigation happened.
*
@ -533,7 +550,11 @@ export class Router {
resolvePromise(false);
} else {
this.routerEvents.next(new NavigationError(id, this.serializeUrl(url), e));
rejectPromise(e);
try {
resolvePromise(this.errorHandler(e));
} catch (ee) {
rejectPromise(ee);
}
}
this.currentRouterState = storedState;
this.currentUrlTree = storedUrl;