Skip to content

Commit

Permalink
Handle errors in EQL search strategy
Browse files Browse the repository at this point in the history
Errors thrown by e.g. the EQL client would not be caught by the search
strategy.
  • Loading branch information
rylnd committed Oct 19, 2020
1 parent a48f02b commit 667d7ff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ describe('EQL search strategy', () => {
expect(mockEqlSearch).not.toHaveBeenCalled();
expect(requestParams).toEqual(expect.objectContaining({ id: 'my-search-id' }));
});

it('emits an error if the client throws', async () => {
expect.assertions(1);
mockEqlSearch.mockReset().mockRejectedValueOnce(new Error('client error'));
const eqlSearch = await eqlSearchStrategyProvider(mockLogger);
eqlSearch.search({ options, params }, {}, mockContext).subscribe(
() => {},
(err) => {
expect(err).toEqual(new Error('client error'));
}
);
});
});

describe('arguments', () => {
Expand Down
77 changes: 41 additions & 36 deletions x-pack/plugins/data_enhanced/server/search/eql_search_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,53 @@ export const eqlSearchStrategyProvider = (
},
search: (request, options, context) =>
from(
new Promise<EqlSearchStrategyResponse>(async (resolve) => {
new Promise<EqlSearchStrategyResponse>(async (resolve, reject) => {
logger.debug(`_eql/search ${JSON.stringify(request.params) || request.id}`);
let promise: TransportRequestPromise<ApiResponse>;
const eqlClient = context.core.elasticsearch.client.asCurrentUser.eql;
const uiSettingsClient = await context.core.uiSettings.client;
const asyncOptions = getAsyncOptions();
const searchOptions = toSnakeCase({ ...request.options });

if (request.id) {
promise = eqlClient.get(
{
id: request.id,
...toSnakeCase(asyncOptions),
},
searchOptions
);
} else {
const { ignoreThrottled, ignoreUnavailable } = await getDefaultSearchParams(
uiSettingsClient
);
const searchParams = toSnakeCase({
ignoreThrottled,
ignoreUnavailable,
...asyncOptions,
...request.params,
});
try {
const eqlClient = context.core.elasticsearch.client.asCurrentUser.eql;
const uiSettingsClient = await context.core.uiSettings.client;
const asyncOptions = getAsyncOptions();
const searchOptions = toSnakeCase({ ...request.options });

promise = eqlClient.search(
searchParams as EqlSearchStrategyRequest['params'],
searchOptions
);
}
if (request.id) {
promise = eqlClient.get(
{
id: request.id,
...toSnakeCase(asyncOptions),
},
searchOptions
);
} else {
const { ignoreThrottled, ignoreUnavailable } = await getDefaultSearchParams(
uiSettingsClient
);
const searchParams = toSnakeCase({
ignoreThrottled,
ignoreUnavailable,
...asyncOptions,
...request.params,
});

promise = eqlClient.search(
searchParams as EqlSearchStrategyRequest['params'],
searchOptions
);
}

const rawResponse = await shimAbortSignal(promise, options?.abortSignal);
const { id, is_partial: isPartial, is_running: isRunning } = rawResponse.body;
const rawResponse = await shimAbortSignal(promise, options?.abortSignal);
const { id, is_partial: isPartial, is_running: isRunning } = rawResponse.body;

resolve({
id,
isPartial,
isRunning,
rawResponse,
});
resolve({
id,
isPartial,
isRunning,
rawResponse,
});
} catch (e) {
reject(e);
}
})
),
};
Expand Down

0 comments on commit 667d7ff

Please sign in to comment.