Skip to content

Commit

Permalink
Improve error logs for task manager poller (elastic#197635)
Browse files Browse the repository at this point in the history
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 <[email protected]>
(cherry picked from commit 81b63c6)
  • Loading branch information
mikecote committed Oct 25, 2024
1 parent ec2bae0 commit 4f42068
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
31 changes: 31 additions & 0 deletions x-pack/plugins/task_manager/server/polling/task_poller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 8 additions & 1 deletion x-pack/plugins/task_manager/server/polling/task_poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4f42068

Please sign in to comment.