Skip to content

Commit

Permalink
[Logs UI] Avoid a crash when a highlight term doesn't exist (#67332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Fernández authored May 27, 2020
1 parent 16a51a1 commit b7e393d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
17 changes: 12 additions & 5 deletions x-pack/plugins/infra/common/http_api/log_entries/highlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,18 @@ export type LogEntriesHighlightsRequest = rt.TypeOf<typeof logEntriesHighlightsR

export const logEntriesHighlightsResponseRT = rt.type({
data: rt.array(
rt.type({
topCursor: logEntriesCursorRT,
bottomCursor: logEntriesCursorRT,
entries: rt.array(logEntryRT),
})
rt.union([
rt.type({
topCursor: rt.null,
bottomCursor: rt.null,
entries: rt.array(logEntryRT),
}),
rt.type({
topCursor: logEntriesCursorRT,
bottomCursor: logEntriesCursorRT,
entries: rt.array(logEntryRT),
}),
])
),
});

Expand Down
20 changes: 15 additions & 5 deletions x-pack/plugins/infra/server/routes/log_entries/highlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,21 @@ export const initLogEntriesHighlightsRoute = ({ framework, logEntries }: InfraBa

return response.ok({
body: logEntriesHighlightsResponseRT.encode({
data: entriesPerHighlightTerm.map((entries) => ({
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) {
Expand Down
28 changes: 28 additions & 0 deletions x-pack/test/api_integration/apis/infra/log_entry_highlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b7e393d

Please sign in to comment.