From b7e393dffb2df78691628ab0cc936240c6195186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez?= Date: Wed, 27 May 2020 16:08:41 +0200 Subject: [PATCH] [Logs UI] Avoid a crash when a highlight term doesn't exist (#67332) --- .../common/http_api/log_entries/highlights.ts | 17 +++++++---- .../server/routes/log_entries/highlights.ts | 20 +++++++++---- .../apis/infra/log_entry_highlights.ts | 28 +++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/infra/common/http_api/log_entries/highlights.ts b/x-pack/plugins/infra/common/http_api/log_entries/highlights.ts index f6d61a7177b4..811cf85db888 100644 --- a/x-pack/plugins/infra/common/http_api/log_entries/highlights.ts +++ b/x-pack/plugins/infra/common/http_api/log_entries/highlights.ts @@ -51,11 +51,18 @@ export type LogEntriesHighlightsRequest = rt.TypeOf ({ - entries, - topCursor: entries[0].cursor, - bottomCursor: entries[entries.length - 1].cursor, - })), + data: entriesPerHighlightTerm.map((entries) => { + if (entries.length > 0) { + return { + entries, + topCursor: entries[0].cursor, + bottomCursor: entries[entries.length - 1].cursor, + }; + } else { + return { + entries, + topCursor: null, + bottomCursor: null, + }; + } + }), }), }); } catch (error) { diff --git a/x-pack/test/api_integration/apis/infra/log_entry_highlights.ts b/x-pack/test/api_integration/apis/infra/log_entry_highlights.ts index e704a8a938dc..823c8159a136 100644 --- a/x-pack/test/api_integration/apis/infra/log_entry_highlights.ts +++ b/x-pack/test/api_integration/apis/infra/log_entry_highlights.ts @@ -46,6 +46,34 @@ export default function ({ getService }: FtrProviderContext) { before(() => esArchiver.load('empty_kibana')); after(() => esArchiver.unload('empty_kibana')); + it('Handles empty responses', async () => { + const { body } = await supertest + .post(LOG_ENTRIES_HIGHLIGHTS_PATH) + .set(COMMON_HEADERS) + .send( + logEntriesHighlightsRequestRT.encode({ + sourceId: 'default', + startTimestamp: KEY_BEFORE_START.time, + endTimestamp: KEY_AFTER_END.time, + highlightTerms: ['some string that does not exist'], + }) + ) + .expect(200); + + const logEntriesHighlightsResponse = pipe( + logEntriesHighlightsResponseRT.decode(body), + fold(throwErrors(createPlainError), identity) + ); + + expect(logEntriesHighlightsResponse.data).to.have.length(1); + + const data = logEntriesHighlightsResponse.data[0]; + + expect(data.entries).to.have.length(0); + expect(data.topCursor).to.be(null); + expect(data.bottomCursor).to.be(null); + }); + it('highlights built-in message column', async () => { const { body } = await supertest .post(LOG_ENTRIES_HIGHLIGHTS_PATH)