Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mgr/dashboard: Introduce API HTTP interceptor to display notification…
…s for more errors than 401, 404 and 500. Signed-off-by: Volker Theile <vtheile@suse.com>
- Loading branch information
Showing
3 changed files
with
50 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters