Skip to content

Commit

Permalink
mgr/dashboard: Introduce API HTTP interceptor to display notification…
Browse files Browse the repository at this point in the history
…s for more errors than 401, 404 and 500.

Signed-off-by: Volker Theile <vtheile@suse.com>
  • Loading branch information
votdev committed Apr 12, 2018
1 parent 0851524 commit 4551a2c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/pybind/mgr/dashboard/frontend/src/app/app.module.ts
Expand Up @@ -10,6 +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 { ApiInterceptorService } from './shared/services/api-interceptor.service';
import { AuthInterceptorService } from './shared/services/auth-interceptor.service';
import { SharedModule } from './shared/shared.module';

Expand Down Expand Up @@ -39,6 +40,11 @@ export class CustomOption extends ToastOptions {
],
exports: [SharedModule],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: ApiInterceptorService,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptorService,
Expand Down
@@ -0,0 +1,37 @@
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 { NotificationService } from './notification.service';

@Injectable()
export class ApiInterceptorService implements HttpInterceptor {
constructor(private router: Router,
public notificationService: NotificationService) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).catch((resp) => {
if (resp instanceof HttpErrorResponse) {
if (resp.status === 404) {
this.router.navigate(['/404']);
}
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);
});
}
}
Expand Up @@ -12,41 +12,21 @@ 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}`
);
}
constructor(private router: Router,
private authStorageService: AuthStorageService,
public notificationService: NotificationService) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).catch(resp => {
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;
if (resp.status === 401) {
this.authStorageService.remove();
this.router.navigate(['/login']);
}
}
// Return the error to the method that called it.
Expand Down

0 comments on commit 4551a2c

Please sign in to comment.