Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/re throw error in http intercepter #1627

Merged
merged 8 commits into from
Jun 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
})
);
}
Expand Down
Loading