U
U
user577652018-05-29 13:34:39
Angular
user57765, 2018-05-29 13:34:39

Angular2 500 error page?

How to make a redirect to a page with a 500 error due to network failures or other problems? Here is my routing for now

import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';

import { DasboardComponent} from '../layout/dasboard.component';

import { SignInComponent } from '../layout/sign-in/sign-in.component';

import { NotFoundComponent } from '../layout/not-found/not-found.component';

import {AuthGuard} from '../shared/guards/auth.guard';

export const routes: Routes = [
  { path: '', redirectTo: 'dasboard', pathMatch: 'full' },
  { path: 'sign_in',   component: SignInComponent },
  { path: 'dasboard',   component: DasboardComponent, canActivate: [AuthGuard] },
  { path: 'not_found',   component: NotFoundComponent },
  { path: '**', component: NotFoundComponent }

];

export const routing: ModuleWithProviders = RouterModule.forRoot(routes, { useHash: true });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Luzanov, 2018-05-29
@dmitry_luzanov

import { Injectable, Injector } from '@angular/core'

import {
  HttpEvent,
  HttpInterceptor,
  HttpHandler,
  HttpRequest
} from '@angular/common/http'

import { Observable } from 'rxjs/Observable'
import 'rxjs/add/observable/throw'
import 'rxjs/add/operator/catch'

@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    console.log('intercepted request ... ')

    return next.handle(req).catch((error, caught) => {
      // Проверяем код ошибки. Берём роутер и перенаправляем куда вам угодно.
      return Observable.throw(error)
    }) as any
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question