[Logs UI] Avoid a crash when a highlight term doesn't exist (#67332)

This commit is contained in:
Alejandro Fernández 2020-05-27 16:08:41 +02:00 committed by GitHub
parent 16a51a18f8
commit b7e393dffb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 10 deletions

View file

@ -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),
}),
])
),
});

View file

@ -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) {

View file

@ -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)