From: Volker Theile Date: Thu, 12 Apr 2018 16:49:28 +0000 (+0200) Subject: mgr/dashboard: Rename auth-interceptor and refactor it to display notifications for... X-Git-Tag: v13.1.0~180^2~2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=8d775bedce26f77e8bf41f6d18edadc20900c207;p=ceph.git mgr/dashboard: Rename auth-interceptor and refactor it to display notifications for more errors than 401 and 500. Signed-off-by: Volker Theile --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/app.module.ts b/src/pybind/mgr/dashboard/frontend/src/app/app.module.ts index d9d6b5f2d84de..4c5974ec304f2 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/app.module.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/app.module.ts @@ -10,7 +10,7 @@ import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { CephModule } from './ceph/ceph.module'; import { CoreModule } from './core/core.module'; -import { AuthInterceptorService } from './shared/services/auth-interceptor.service'; +import { ApiInterceptorService } from './shared/services/api-interceptor.service'; import { SharedModule } from './shared/shared.module'; export class CustomOption extends ToastOptions { @@ -41,7 +41,7 @@ export class CustomOption extends ToastOptions { providers: [ { provide: HTTP_INTERCEPTORS, - useClass: AuthInterceptorService, + useClass: ApiInterceptorService, multi: true }, { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts new file mode 100644 index 0000000000000..d412119bbc289 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts @@ -0,0 +1,47 @@ +import { + HttpErrorResponse, + HttpEvent, + HttpHandler, + HttpInterceptor, + HttpRequest +} from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; + +import 'rxjs/add/observable/throw'; +import 'rxjs/add/operator/catch'; +import { Observable } from 'rxjs/Observable'; + +import { NotificationType } from '../enum/notification-type.enum'; +import { AuthStorageService } from './auth-storage.service'; +import { NotificationService } from './notification.service'; + +@Injectable() +export class ApiInterceptorService implements HttpInterceptor { + + constructor(private router: Router, + private authStorageService: AuthStorageService, + public notificationService: NotificationService) {} + + intercept(request: HttpRequest, next: HttpHandler): Observable> { + return next.handle(request).catch(resp => { + if (resp instanceof HttpErrorResponse) { + switch (resp.status) { + case 401: + this.authStorageService.remove(); + this.router.navigate(['/login']); + break; + case 404: + this.router.navigate(['/404']); + break; + } + this.notificationService.show( + NotificationType.error, + resp.error.detail || '', + `${resp.status} - ${resp.statusText}`); + } + // Return the error to the method that called it. + return Observable.throw(resp); + }); + } +} 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 deleted file mode 100644 index 69a6d57343258..0000000000000 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-interceptor.service.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { - HttpErrorResponse, - HttpEvent, - HttpHandler, - HttpInterceptor, - HttpRequest -} from '@angular/common/http'; -import { Injectable } from '@angular/core'; -import { Router } from '@angular/router'; - -import 'rxjs/add/observable/throw'; -import 'rxjs/add/operator/catch'; -import { Observable } from 'rxjs/Observable'; - -import { NotificationType } from '../enum/notification-type.enum'; -import { AuthStorageService } from './auth-storage.service'; -import { NotificationService } from './notification.service'; - -@Injectable() -export class AuthInterceptorService implements HttpInterceptor { - constructor( - private router: Router, - private authStorageService: AuthStorageService, - public notificationService: NotificationService - ) {} - - _notify (resp) { - this.notificationService.show( - NotificationType.error, - resp.error.detail || '', - `${resp.status} - ${resp.statusText}` - ); - } - - intercept(request: HttpRequest, next: HttpHandler): Observable> { - 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']); - this._notify(resp); - break; - case 500: - this._notify(resp); - break; - } - } - // Return the error to the method that called it. - return Observable.throw(resp); - }); - } -}