Skip to content

Commit

Permalink
avoid error when logging invalid response error (#75757)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet authored Aug 24, 2020
1 parent 3dea444 commit 9fa43b4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
38 changes: 38 additions & 0 deletions src/core/server/elasticsearch/client/configure_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,44 @@ describe('configureClient', () => {
`);
});

it('logs default error info when the error response body is empty', () => {
const client = configureClient(config, { logger, scoped: false });

let response = createApiResponse({
statusCode: 400,
headers: {},
body: {
error: {},
},
});
client.emit('response', new errors.ResponseError(response), response);

expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(`
Array [
Array [
"[ResponseError]: Response Error",
],
]
`);

logger.error.mockClear();

response = createApiResponse({
statusCode: 400,
headers: {},
body: {} as any,
});
client.emit('response', new errors.ResponseError(response), response);

expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(`
Array [
Array [
"[ResponseError]: Response Error",
],
]
`);
});

it('logs each queries if `logQueries` is true', () => {
const client = configureClient(
createFakeConfig({
Expand Down
7 changes: 2 additions & 5 deletions src/core/server/elasticsearch/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { stringify } from 'querystring';
import { Client } from '@elastic/elasticsearch';
import { Logger } from '../../logging';
import { parseClientOptions, ElasticsearchClientConfig } from './client_config';
import { isResponseError } from './errors';

export const configureClient = (
config: ElasticsearchClientConfig,
Expand All @@ -39,10 +38,8 @@ const addLogging = (client: Client, logger: Logger, logQueries: boolean) => {
client.on('response', (error, event) => {
if (error) {
const errorMessage =
// error details for response errors provided by elasticsearch
isResponseError(error)
? `[${event.body.error.type}]: ${event.body.error.reason}`
: `[${error.name}]: ${error.message}`;
// error details for response errors provided by elasticsearch, defaults to error name/message
`[${event.body?.error?.type ?? error.name}]: ${event.body?.error?.reason ?? error.message}`;

logger.error(errorMessage);
}
Expand Down

0 comments on commit 9fa43b4

Please sign in to comment.