Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error logs for task manager poller #197635

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
typeof err === 'string' ? new Error(err) : err
)
);
}

export class PollingError<T> extends Error {
Expand Down