Improve error logs for task manager poller (#197635)

I noticed some scenarios we see error logs from the task poller like
`Failed to poll for work: undefined` making me think `err.message` is
empty in some situations. I'm modifying the code to handle string
situations if ever they occur by performing `err.message || err` and to
also include a stack trace when strings are passed-in.

---------

Co-authored-by: Patrick Mueller <patrick.mueller@elastic.co>
This commit is contained in:
Mike Côté 2024-10-25 07:12:44 -04:00 committed by GitHub
parent d3569f609a
commit 81b63c60eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View file

@ -266,6 +266,37 @@ describe('TaskPoller', () => {
expect(handler.mock.calls[0][0].error.stack).toContain(workError.stack);
});
test('still logs errors when they are thrown as strings', async () => {
const pollInterval = 100;
const handler = jest.fn();
const workError = 'failed to work';
const poller = createTaskPoller<string, string[]>({
initialPollInterval: pollInterval,
logger: loggingSystemMock.create().get(),
pollInterval$: of(pollInterval),
pollIntervalDelay$: of(0),
work: async (...args) => {
throw workError;
},
getCapacity: () => 5,
});
poller.events$.subscribe(handler);
poller.start();
clock.tick(pollInterval);
await new Promise((resolve) => setImmediate(resolve));
const expectedError = new PollingError<string>(
'Failed to poll for work: failed to work',
PollingErrorType.WorkError,
none
);
expect(handler).toHaveBeenCalledWith(asErr(expectedError));
expect(handler.mock.calls[0][0].error.type).toEqual(PollingErrorType.WorkError);
expect(handler.mock.calls[0][0].error.stack).toBeDefined();
});
test('continues polling after work fails', async () => {
const pollInterval = 100;

View file

@ -151,7 +151,14 @@ export enum PollingErrorType {
}
function asPollingError<T>(err: Error, type: PollingErrorType, data: Option<T> = none) {
return asErr(new PollingError<T>(`Failed to poll for work: ${err.message}`, type, data, err));
return asErr(
new PollingError<T>(
`Failed to poll for work: ${err.message || err}`,
type,
data,
err instanceof Error ? err : new Error(`${err}`)
)
);
}
export class PollingError<T> extends Error {