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 46a2b5bb734f1e39c5bbbe737419d93188aebe0c..62050114b77300216fa55416f9d91eb6c8f496e5 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 436c31f89a45f30ee186319432ffe609711ed6bb..089df51404ba39feca180d70c486bb5a5824a533 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 {