From 27d0768ab3d6804bb969a20558fec0e0b74c8d97 Mon Sep 17 00:00:00 2001 From: danoswaltCL <97542869+danoswaltCL@users.noreply.github.com> Date: Wed, 12 Jun 2024 10:49:59 -0400 Subject: [PATCH] Bugfix/re throw error in http intercepter (#1627) * rethrow error in interceptor instead of empty * rethrow error in interceptor instead of empty * just throw err, no need for returning observable --- .../http-error.interceptor.spec.ts | 27 ++++++++++--------- .../http-error.interceptor.ts | 6 +++-- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/frontend/projects/upgrade/src/app/core/http-interceptors/http-error.interceptor.spec.ts b/frontend/projects/upgrade/src/app/core/http-interceptors/http-error.interceptor.spec.ts index ccfbcedcea..adbe13befe 100644 --- a/frontend/projects/upgrade/src/app/core/http-interceptors/http-error.interceptor.spec.ts +++ b/frontend/projects/upgrade/src/app/core/http-interceptors/http-error.interceptor.spec.ts @@ -78,40 +78,43 @@ describe('HttpErrorInterceptor', () => { describe('#intercept', () => { it('should logout if a 401 error is caught and open popup', fakeAsync(() => { - const mockError = { - status: 401, - }; const mockRequest: any = {}; + const mockError = { status: 401, message: 'test' }; const mockNextHandler = { - handle: jest.fn().mockReturnValue(throwError(mockError)), + handle: jest.fn().mockReturnValue(throwError(() => mockError)), }; mockAuthService.authLogout = jest.fn(); service.openPopup = jest.fn(); - service.intercept(mockRequest, mockNextHandler).subscribe(); + service.intercept(mockRequest, mockNextHandler).subscribe({ + error: (error: Error) => { + expect(error).toEqual(mockError); + }, + }); tick(0); - expect(service.openPopup).toHaveBeenCalledWith(mockError); expect(mockAuthService.authLogout).toHaveBeenCalled(); })); it('should NOT logout if error is not 401, and open popup', fakeAsync(() => { - const mockError = { - status: 400, - }; + const mockError = { status: 400, message: 'test' }; const mockRequest: any = {}; const mockNextHandler = { - handle: jest.fn().mockReturnValue(throwError(mockError)), + handle: jest.fn().mockReturnValue(throwError(() => mockError)), }; mockAuthService.authLogout = jest.fn(); service.openPopup = jest.fn(); - service.intercept(mockRequest, mockNextHandler).subscribe(); + service.intercept(mockRequest, mockNextHandler).subscribe({ + error: (error: Error) => { + expect(error).toEqual(mockError); + }, + }); tick(0); - expect(service.openPopup).toHaveBeenCalledWith(mockError); + expect(service.openPopup).toHaveBeenCalled(); expect(mockAuthService.authLogout).not.toHaveBeenCalled(); })); diff --git a/frontend/projects/upgrade/src/app/core/http-interceptors/http-error.interceptor.ts b/frontend/projects/upgrade/src/app/core/http-interceptors/http-error.interceptor.ts index c8aa7faa2f..1c3a9644d9 100755 --- a/frontend/projects/upgrade/src/app/core/http-interceptors/http-error.interceptor.ts +++ b/frontend/projects/upgrade/src/app/core/http-interceptors/http-error.interceptor.ts @@ -1,7 +1,7 @@ import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; import { Inject, Injectable } from '@angular/core'; import { NotificationsService, NotificationType } from 'angular2-notifications'; -import { EMPTY, Observable } from 'rxjs'; +import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { ENV, Environment } from '../../../environments/environment-types'; import { AuthService } from '../auth/auth.service'; @@ -32,7 +32,9 @@ export class HttpErrorInterceptor implements HttpInterceptor { this.authService.authLogout(); } this.openPopup(err); - return EMPTY; // returning EMPTY instead of throwError as Error is handled using snacker here itself + + // re-throw to allow the error to be caught by the calling code + throw err; }) ); }