From 9ad4a71a4fb9c2e39ae83936b0a096179f492fb2 Mon Sep 17 00:00:00 2001 From: Dominique Clarke Date: Wed, 5 Oct 2022 08:16:39 -0400 Subject: [PATCH] uptime - handle index not found errors (#142685) --- x-pack/plugins/uptime/server/lib/lib.ts | 2 +- .../server/lib/requests/get_index_status.ts | 38 ++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/lib.ts b/x-pack/plugins/uptime/server/lib/lib.ts index 1b3e8200cde9c..32227bf39f732 100644 --- a/x-pack/plugins/uptime/server/lib/lib.ts +++ b/x-pack/plugins/uptime/server/lib/lib.ts @@ -93,7 +93,7 @@ export function createUptimeESClient({ esError, esRequestParams: esParams, esRequestStatus, - esResponse: res.body, + esResponse: res?.body, kibanaRequest: request!, operationName: operationName ?? '', startTime: startTimeNow, diff --git a/x-pack/plugins/uptime/server/lib/requests/get_index_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_index_status.ts index dcd61d5331aa4..1eef2f10aeeb2 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_index_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_index_status.ts @@ -11,18 +11,30 @@ import { StatesIndexStatus } from '../../../common/runtime_types'; export const getIndexStatus: UMElasticsearchQueryFn<{}, StatesIndexStatus> = async ({ uptimeEsClient, }) => { - const { - indices, - result: { - body: { - _shards: { total }, - count, + try { + const { + indices, + result: { + body: { + _shards: { total }, + count, + }, }, - }, - } = await uptimeEsClient.count({ terminateAfter: 1 }); - return { - indices, - indexExists: total > 0, - docCount: count, - }; + } = await uptimeEsClient.count({ terminateAfter: 1 }); + return { + indices, + indexExists: total > 0, + docCount: count, + }; + } catch (e) { + if (e.meta.statusCode === 404) { + // we don't throw an error for index not found + return { + indices: '', + indexExists: false, + docCount: 0, + }; + } + throw e; + } };