Angular2 How to display error page while saving route

Sorry if this is somewhere else, but my usual search tactics didn't help me.

I want to display a custom error page when I get 500 from the API. I use restangular and have a response hook that redirects to the error page when I get 500 using:

this.router.navigate(['/error']);

It all works. However, I would like to keep the previous URL in the browser navigation bar so that when updating the user, he will try to execute the previous page again, while right now they must re-go to the error page in order to try again.

Thanks!

+6
source share
3 answers

. localStorage, request param /error?page=redirectUrl.

, , , localStorage.

+1

:

setRouteErrorHandler(): void {
    let errorRoute = null;

    this.router.errorHandler = (error): void => {
        this.location.go(errorRoute.url);
        this.router.navigate(['/error'], { skipLocationChange: true, replaceUrl: true });
    };

    this.router.events
        .filter((next) => next instanceOf NavigationError)
        .subscribe((next) => {
            errorRoute = next;
    });
}

setRouteErrorHandler() AppModule APP_INITIALIZER

+1

ErrorComponent, , .

, ,

try {

}
catch (e) {

}
-1

Source: https://habr.com/ru/post/1017395/


All Articles