From cd08b58a6640bd5b02fc7be9e46b359b15802c00 Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Mon, 27 Sep 2021 15:02:48 +0200 Subject: [PATCH] Add missing error handlers for deprecation logging route --- .../server/routes/deprecation_logging.test.ts | 2 + .../server/routes/deprecation_logging.ts | 61 ++++++++++++------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts b/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts index 0aca90ddf9610..1a4b55447f4ad 100644 --- a/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts @@ -8,6 +8,7 @@ import { kibanaResponseFactory } from 'src/core/server'; import { createMockRouter, MockRouter, routeHandlerContextMock } from './__mocks__/routes.mock'; import { createRequestMock } from './__mocks__/request.mock'; +import { handleEsError } from '../shared_imports'; jest.mock('../lib/es_version_precheck', () => ({ versionCheckHandlerWrapper: (a: any) => a, @@ -28,6 +29,7 @@ describe('deprecation logging API', () => { mockRouter = createMockRouter(); routeDependencies = { router: mockRouter, + lib: { handleEsError }, }; registerDeprecationLoggingRoutes(routeDependencies); }); diff --git a/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts b/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts index a0dfe0d152ad5..00116ae24a3fb 100644 --- a/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts +++ b/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts @@ -16,7 +16,10 @@ import { versionCheckHandlerWrapper } from '../lib/es_version_precheck'; import { RouteDependencies } from '../types'; import { DEPRECATION_LOGS_INDEX } from '../../common/constants'; -export function registerDeprecationLoggingRoutes({ router }: RouteDependencies) { +export function registerDeprecationLoggingRoutes({ + router, + lib: { handleEsError }, +}: RouteDependencies) { router.get( { path: `${API_BASE_PATH}/deprecation_logging`, @@ -32,8 +35,12 @@ export function registerDeprecationLoggingRoutes({ router }: RouteDependencies) request, response ) => { - const result = await getDeprecationLoggingStatus(client); - return response.ok({ body: result }); + try { + const result = await getDeprecationLoggingStatus(client); + return response.ok({ body: result }); + } catch (error) { + return handleEsError({ error, response }); + } } ) ); @@ -57,10 +64,14 @@ export function registerDeprecationLoggingRoutes({ router }: RouteDependencies) request, response ) => { - const { isEnabled } = request.body as { isEnabled: boolean }; - return response.ok({ - body: await setDeprecationLogging(client, isEnabled), - }); + try { + const { isEnabled } = request.body as { isEnabled: boolean }; + return response.ok({ + body: await setDeprecationLogging(client, isEnabled), + }); + } catch (error) { + return handleEsError({ error, response }); + } } ) ); @@ -84,28 +95,32 @@ export function registerDeprecationLoggingRoutes({ router }: RouteDependencies) request, response ) => { - const { body: indexExists } = await client.asCurrentUser.indices.exists({ - index: DEPRECATION_LOGS_INDEX, - }); + try { + const { body: indexExists } = await client.asCurrentUser.indices.exists({ + index: DEPRECATION_LOGS_INDEX, + }); - if (!indexExists) { - return response.ok({ body: { count: 0 } }); - } + if (!indexExists) { + return response.ok({ body: { count: 0 } }); + } - const { body } = await client.asCurrentUser.count({ - index: DEPRECATION_LOGS_INDEX, - body: { - query: { - range: { - '@timestamp': { - gte: request.query.from, + const { body } = await client.asCurrentUser.count({ + index: DEPRECATION_LOGS_INDEX, + body: { + query: { + range: { + '@timestamp': { + gte: request.query.from, + }, }, }, }, - }, - }); + }); - return response.ok({ body: { count: body.count } }); + return response.ok({ body: { count: body.count } }); + } catch (error) { + return handleEsError({ error, response }); + } } ) );