From 18e5e461aaf1434b11ad07c1b3cbe3f490412057 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Fri, 16 Mar 2018 11:10:01 +0100 Subject: [PATCH] mgr/dashboard: Improve auth interceptor. In case of an error the response must be returned. Signed-off-by: Volker Theile --- .../services/auth-interceptor.service.ts | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts index f09250d6c172e..cc7cffe8c2c38 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts @@ -1,12 +1,12 @@ import { - HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, - HttpResponse + HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { ToastsManager } from 'ng2-toastr'; -import 'rxjs/add/operator/do'; +import 'rxjs/add/observable/throw'; +import 'rxjs/add/operator/catch'; import { Observable } from 'rxjs/Observable'; import { AuthStorageService } from './auth-storage.service'; @@ -20,23 +20,24 @@ export class AuthInterceptorService implements HttpInterceptor { } intercept(request: HttpRequest, next: HttpHandler): Observable> { - return next.handle(request).do((event: HttpEvent) => { - if (event instanceof HttpResponse) { - // do nothing - } - }, (err: any) => { - if (err instanceof HttpErrorResponse) { - if (err.status === 404) { - this.router.navigate(['/404']); - return; + return next.handle(request) + .catch((resp) => { + if (resp instanceof HttpErrorResponse) { + switch (resp.status) { + case 404: + this.router.navigate(['/404']); + break; + case 401: + this.authStorageService.remove(); + this.router.navigate(['/login']); + // falls through + default: + this.toastr.error(resp.error.detail || '', + `${resp.status} - ${resp.statusText}`); + } } - - this.toastr.error(err.error.detail || '', `${err.status} - ${err.statusText}`); - if (err.status === 401) { - this.authStorageService.remove(); - this.router.navigate(['/login']); - } - } - }); + // Return the error to the method that called it. + return Observable.throw(resp); + }); } } -- 2.39.5