Skip to content

Commit

Permalink
feat: catch handleAction in workspace log interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederson committed Dec 19, 2024
1 parent 9dedc00 commit c3c4fec
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,37 @@ describe('WorkspacesLogsInterceptor', () => {
'[WORKSPACE/LOGS] Invalid log action: INVALID_ACTION',
);
});

it('When handleAction fails then should log an error', async () => {
Reflect.defineMetadata(
'workspaceLogAction',
WorkspaceLogType.Login,
mockHandler,
);

const next: CallHandler = {
handle: jest.fn().mockReturnValue(of({})),
};

const handleActionSpy = jest
.spyOn(interceptor, 'handleAction')
.mockRejectedValueOnce(new Error('Logging failed'));

const logErrorSpy = jest.spyOn(Logger, 'error').mockImplementation();

await lastValueFrom(interceptor.intercept(context, next));

expect(handleActionSpy).toHaveBeenCalled();
expect(logErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('Error logging action: Logging failed'),
);
expect(logErrorSpy).toHaveBeenCalledWith(
expect.stringContaining(`Platform: ${WorkspaceLogPlatform.Web}`),
);
expect(logErrorSpy).toHaveBeenCalledWith(
expect.stringContaining(`Action: ${WorkspaceLogType.Login}`),
);
});
});

describe('handleAction()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ export class WorkspacesLogsInterceptor implements NestInterceptor {

return next.handle().pipe(
tap((res) => {
this.handleAction(platform, this.logAction, request, res);
this.handleAction(platform, this.logAction, request, res).catch((err) =>
Logger.error(
`[WORKSPACE/LOGS] Error logging action: ${
err.message || err
}. Platform: ${platform}, Action: ${this.logAction}.`,
),
);
}),
);
}
Expand Down

0 comments on commit c3c4fec

Please sign in to comment.