Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 21x 21x 21x 36x 36x 14x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 10x 14x 4x 14x 1x 1x 14x 4x 10x 6x 6x 1x 5x 1x 4x 4x 6x 4x | import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import * as _ from 'lodash'; import { Observable, throwError as observableThrowError } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { NotificationType } from '../enum/notification-type.enum'; import { CdNotificationConfig } from '../models/cd-notification'; import { FinishedTask } from '../models/finished-task'; import { AuthStorageService } from './auth-storage.service'; import { NotificationService } from './notification.service'; @Injectable({ providedIn: 'root' }) export class ApiInterceptorService implements HttpInterceptor { constructor( private router: Router, private authStorageService: AuthStorageService, public notificationService: NotificationService ) {} intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request).pipe( catchError((resp) => { Eif (resp instanceof HttpErrorResponse) { let timeoutId: number; switch (resp.status) { case 400: const finishedTask = new FinishedTask(); const task = resp.error.task; Eif (_.isPlainObject(task)) { task.metadata.component = task.metadata.component || resp.error.component; finishedTask.name = task.name; finishedTask.metadata = task.metadata; } else { finishedTask.metadata = resp.error; } finishedTask.success = false; finishedTask.exception = resp.error; timeoutId = this.notificationService.notifyTask(finishedTask); break; case 401: this.authStorageService.remove(); this.router.navigate(['/login']); break; case 403: this.router.navigate(['/403']); break; default: timeoutId = this.prepareNotification(resp); } /** * Decorated preventDefault method (in case error previously had * preventDefault method defined). If called, it will prevent a * notification to be shown. */ resp['preventDefault'] = () => { this.notificationService.cancel(timeoutId); }; /** * If called, it will prevent a notification for the specific status code. * @param {number} status The status code to be ignored. */ resp['ignoreStatusCode'] = function(status: number) { Eif (this.status === status) { this.preventDefault(); } }; } // Return the error to the method that called it. return observableThrowError(resp); }) ); } private prepareNotification(resp): number { return this.notificationService.show(() => { let message = ''; if (_.isPlainObject(resp.error) && _.isString(resp.error.detail)) { message = resp.error.detail; // Error was triggered by the backend. } else if (_.isString(resp.error)) { message = resp.error; } else Eif (_.isString(resp.message)) { message = resp.message; } return new CdNotificationConfig( NotificationType.error, `${resp.status} - ${resp.statusText}`, message, undefined, resp['application'] ); }); } } |