Skip to content

Commit

Permalink
feat(component-store): handle errors in next callback (#3533)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-okrushko authored Aug 16, 2022
1 parent 1a906fd commit 551c8eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions modules/component-store/spec/tap-response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ describe('tapResponse', () => {
expect(errorCallback).toHaveBeenCalledWith(error);
});

it('should invoke error callback on the exception thrown in next', () => {
const errorCallback = jest.fn<void, [{ message: string }]>();
const error = { message: 'error' };

function producesError() {
throw error;
}

of(1).pipe(tapResponse(producesError, errorCallback)).subscribe();

expect(errorCallback).toHaveBeenCalledWith(error);
});

it('should invoke complete callback on complete', () => {
const completeCallback = jest.fn<void, []>();

Expand Down
6 changes: 4 additions & 2 deletions modules/component-store/src/tap-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export function tapResponse<T, E = unknown>(
source.pipe(
tap({
next: nextFn,
error: errorFn,
complete: completeFn,
}),
catchError(() => EMPTY)
catchError((e) => {
errorFn(e);
return EMPTY;
})
);
}

0 comments on commit 551c8eb

Please sign in to comment.