]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Improve auth interceptor. 20847/head
authorVolker Theile <vtheile@suse.com>
Fri, 16 Mar 2018 10:10:01 +0000 (11:10 +0100)
committerVolker Theile <vtheile@suse.com>
Thu, 22 Mar 2018 09:42:47 +0000 (10:42 +0100)
In case of an error the response must be returned.

Signed-off-by: Volker Theile <vtheile@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts

index f09250d6c172e09181fe38c27057a72448637189..cc7cffe8c2c38d6648aeca80990b5ce4ae25441f 100644 (file)
@@ -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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
-    return next.handle(request).do((event: HttpEvent<any>) => {
-      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);
+      });
   }
 }