Skip to content

Commit

Permalink
Bugfix/re throw error in http intercepter (#1627)
Browse files Browse the repository at this point in the history
* rethrow error in interceptor instead of empty

* rethrow error in interceptor instead of empty

* just throw err, no need for returning observable
  • Loading branch information
danoswaltCL authored Jun 12, 2024
1 parent ff3c924 commit 27d0768
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
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

0 comments on commit 27d0768

Please sign in to comment.