mgr/dashboard: Rename auth-interceptor and refactor it to display notifications for more errors than 401 and 500.

Signed-off-by: Volker Theile <vtheile@suse.com>
This commit is contained in:
Volker Theile 2018-04-12 18:49:28 +02:00
parent eaa02bc722
commit 8d775bedce
2 changed files with 12 additions and 21 deletions

View File

@ -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
},
{

View File

@ -17,37 +17,28 @@ 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
) {}
export class ApiInterceptorService implements HttpInterceptor {
_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 => {
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);
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);