From 1b842e8a249c049cc6733273c4c8e8ba1aa66326 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Wed, 10 Nov 2021 18:15:28 +0100 Subject: [PATCH] OZG-1389 OZG-1709 extend error interceptor --- .../http-error.interceptor.spec.ts | 41 ++++++++++++++++++- .../lib/interceptor/http-error.interceptor.ts | 22 +++++++--- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/goofy-client/libs/ui/src/lib/interceptor/http-error.interceptor.spec.ts b/goofy-client/libs/ui/src/lib/interceptor/http-error.interceptor.spec.ts index 46a2b5bb73..62050114b7 100644 --- a/goofy-client/libs/ui/src/lib/interceptor/http-error.interceptor.spec.ts +++ b/goofy-client/libs/ui/src/lib/interceptor/http-error.interceptor.spec.ts @@ -1,6 +1,7 @@ import { HttpStatusCode } from '@angular/common/http'; import { TestBed } from '@angular/core/testing'; import { MatDialogModule } from '@angular/material/dialog'; +import { HttpErrorHandler } from '@goofy-client/tech-shared'; import { mock } from '@goofy-client/test-utils'; import { createApiError } from 'libs/tech-shared/test/error'; import { Subject } from 'rxjs'; @@ -14,6 +15,7 @@ describe('HttpErrorInterceptor', () => { const dialogService = mock(DialogService); const snackbarService = mock(SnackBarService); + const httpErrorHandler = mock(HttpErrorHandler); beforeEach(() => TestBed.configureTestingModule({ imports: [ @@ -28,6 +30,10 @@ describe('HttpErrorInterceptor', () => { { provide: SnackBarService, useValue: snackbarService + }, + { + provide: HttpErrorHandler, + useValue: httpErrorHandler } ] })); @@ -47,14 +53,45 @@ describe('HttpErrorInterceptor', () => { const subject: Subject<any> = new Subject(); const httpHandler = { handle: () => subject }; - it('should call handleError with error', () => { - interceptor.handleError = jest.fn(); + it('should call http error handler', () => { + interceptor.handleErrorResponse = jest.fn(); interceptor.intercept(<any>{ event }, <any>httpHandler).subscribe(); subject.error(event); + expect(httpErrorHandler.shouldDoDefaultHandling).toHaveBeenCalled(); + }) + + it('should call handleErrorResponse with error', () => { + httpErrorHandler.shouldDoDefaultHandling.mockReturnValue(true); + interceptor.handleErrorResponse = jest.fn(); + + interceptor.intercept(<any>{ event }, <any>httpHandler).subscribe(); + subject.error(event); + + expect(interceptor.handleErrorResponse).toHaveBeenCalledWith(response, true); + }) + }) + + describe('handleErrorResponse', () => { + + const response = <any>{ error: createApiError(), status: 500 }; + + beforeEach(() => { + interceptor.handleError = jest.fn(); + }) + + it('should call handleError if defaultHandling is enabled', () => { + interceptor.handleErrorResponse(response, true); + expect(interceptor.handleError).toHaveBeenCalledWith(response); }) + + it('should not call handleError if defaultHandling is disabled', () => { + interceptor.handleErrorResponse(response, false); + + expect(interceptor.handleError).not.toHaveBeenCalled(); + }) }) describe('handleError', () => { diff --git a/goofy-client/libs/ui/src/lib/interceptor/http-error.interceptor.ts b/goofy-client/libs/ui/src/lib/interceptor/http-error.interceptor.ts index 436c31f89a..089df51404 100644 --- a/goofy-client/libs/ui/src/lib/interceptor/http-error.interceptor.ts +++ b/goofy-client/libs/ui/src/lib/interceptor/http-error.interceptor.ts @@ -1,22 +1,33 @@ import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { ApiError, isForbidden, isServerError, isUnauthorized } from '@goofy-client/tech-shared'; +import { ApiError, HttpErrorHandler, isForbidden, isServerError, isUnauthorized } from '@goofy-client/tech-shared'; import { SnackBarService } from '@goofy-client/ui'; import { Observable, throwError } from 'rxjs'; -import { catchError } from 'rxjs/operators'; +import { catchError, finalize } from 'rxjs/operators'; import { DialogService } from '../ui/dialog/dialog.service'; import { Messages } from '../ui/messages'; @Injectable() export class HttpErrorInterceptor implements HttpInterceptor { - constructor(private dialogService: DialogService, private snackbarService: SnackBarService) { } + constructor(private dialogService: DialogService, private snackbarService: SnackBarService, private errorHandler: HttpErrorHandler) { } intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { - return next.handle(request).pipe(catchError((error: any) => this.handleError(error.response))); + const defaultHandling: boolean = this.errorHandler.shouldDoDefaultHandling(); + return next.handle(request).pipe( + catchError((error: any) => this.handleErrorResponse(error.response, defaultHandling)), + finalize(() => this.errorHandler.enableDefaultHandling()) + ); } - handleError(errorResponse: HttpErrorResponse): Observable<any> { + handleErrorResponse(errorResponse: HttpErrorResponse, defaultHandling: boolean): Observable<any> { + if (defaultHandling) { + this.handleError(errorResponse); + } + return throwError({ error: errorResponse }); + } + + handleError(errorResponse: HttpErrorResponse): void { if (isServerError(errorResponse.status)) { this.handleServerError(errorResponse.error); } @@ -26,7 +37,6 @@ export class HttpErrorInterceptor implements HttpInterceptor { if (isForbidden(errorResponse.status)) { this.handleForbiddenError(); } - return throwError({ error: errorResponse }); } reloadWindow(): void { -- GitLab