From e1e14018da275c2d751359b2d3925c49b021ad49 Mon Sep 17 00:00:00 2001 From: Jean-Louis Leysens Date: Wed, 11 Nov 2020 17:35:32 +0100 Subject: [PATCH] [Grokdebugger] Fix simulate error handling (#83036) (#83171) * Fix detection of 4xx errors in grokdebugger simulate endpoint - removed code to call simulate endpoint from "KibanaFramework" - fixed throwing of string value - using new elasticsearch client instead of legacy - handle error with shared error handling logic * added deprecation notice to register route on KibanaFramework * remove deprecation notice --- .../grokdebugger/grokdebugger_service.js | 2 +- .../server/lib/kibana_framework.ts | 26 +------------------ ...ute.js => register_grok_simulate_route.ts} | 21 +++++++++------ .../grokdebugger/server/shared_imports.ts | 7 +++++ 4 files changed, 22 insertions(+), 34 deletions(-) rename x-pack/plugins/grokdebugger/server/routes/api/grokdebugger/{register_grok_simulate_route.js => register_grok_simulate_route.ts} (73%) create mode 100644 x-pack/plugins/grokdebugger/server/shared_imports.ts diff --git a/x-pack/plugins/grokdebugger/public/services/grokdebugger/grokdebugger_service.js b/x-pack/plugins/grokdebugger/public/services/grokdebugger/grokdebugger_service.js index 207093e72ca2c..7f4ac35bd4eef 100644 --- a/x-pack/plugins/grokdebugger/public/services/grokdebugger/grokdebugger_service.js +++ b/x-pack/plugins/grokdebugger/public/services/grokdebugger/grokdebugger_service.js @@ -21,7 +21,7 @@ export class GrokdebuggerService { return GrokdebuggerResponse.fromUpstreamJSON(response); }) .catch((e) => { - throw e.body.message; + throw new Error(e.body.message); }); } } diff --git a/x-pack/plugins/grokdebugger/server/lib/kibana_framework.ts b/x-pack/plugins/grokdebugger/server/lib/kibana_framework.ts index ee7fa74022fd5..94b7f8a9af796 100644 --- a/x-pack/plugins/grokdebugger/server/lib/kibana_framework.ts +++ b/x-pack/plugins/grokdebugger/server/lib/kibana_framework.ts @@ -6,14 +6,7 @@ import { i18n } from '@kbn/i18n'; -import { - CoreSetup, - IRouter, - RequestHandlerContext, - RouteMethod, - RouteConfig, - RequestHandler, -} from 'src/core/server'; +import { CoreSetup, IRouter, RouteMethod, RouteConfig, RequestHandler } from 'src/core/server'; import { ILicense } from '../../../licensing/server'; @@ -83,21 +76,4 @@ export class KibanaFramework { break; } } - - callWithRequest( - requestContext: RequestHandlerContext, - endpoint: 'ingest.simulate', - options?: { - body: any; - } - ): Promise; - - public async callWithRequest( - requestContext: RequestHandlerContext, - endpoint: string, - options?: any - ) { - const { elasticsearch } = requestContext.core; - return elasticsearch.legacy.client.callAsCurrentUser(endpoint, options); - } } diff --git a/x-pack/plugins/grokdebugger/server/routes/api/grokdebugger/register_grok_simulate_route.js b/x-pack/plugins/grokdebugger/server/routes/api/grokdebugger/register_grok_simulate_route.ts similarity index 73% rename from x-pack/plugins/grokdebugger/server/routes/api/grokdebugger/register_grok_simulate_route.js rename to x-pack/plugins/grokdebugger/server/routes/api/grokdebugger/register_grok_simulate_route.ts index f953bc64c3b4f..3525de8da87d1 100644 --- a/x-pack/plugins/grokdebugger/server/routes/api/grokdebugger/register_grok_simulate_route.js +++ b/x-pack/plugins/grokdebugger/server/routes/api/grokdebugger/register_grok_simulate_route.ts @@ -5,9 +5,16 @@ */ import { schema } from '@kbn/config-schema'; + +// @ts-ignore import { GrokdebuggerRequest } from '../../../models/grokdebugger_request'; +// @ts-ignore import { GrokdebuggerResponse } from '../../../models/grokdebugger_response'; +import { handleEsError } from '../../../shared_imports'; + +import { KibanaFramework } from '../../../lib/kibana_framework'; + const requestBodySchema = schema.object({ pattern: schema.string(), rawEvent: schema.string(), @@ -15,7 +22,7 @@ const requestBodySchema = schema.object({ customPatterns: schema.object({}, { unknowns: 'allow' }), }); -export function registerGrokSimulateRoute(framework) { +export function registerGrokSimulateRoute(framework: KibanaFramework) { framework.registerRoute( { method: 'post', @@ -27,19 +34,17 @@ export function registerGrokSimulateRoute(framework) { async (requestContext, request, response) => { try { const grokdebuggerRequest = GrokdebuggerRequest.fromDownstreamJSON(request.body); - const simulateResponseFromES = await framework.callWithRequest( - requestContext, - 'ingest.simulate', + const simulateResponseFromES = await requestContext.core.elasticsearch.client.asCurrentUser.ingest.simulate( { body: grokdebuggerRequest.upstreamJSON } ); - const grokdebuggerResponse = GrokdebuggerResponse.fromUpstreamJSON(simulateResponseFromES); + const grokdebuggerResponse = GrokdebuggerResponse.fromUpstreamJSON( + simulateResponseFromES.body + ); return response.ok({ body: grokdebuggerResponse, }); } catch (error) { - return response.internalError({ - body: error.message, - }); + return handleEsError({ error, response }); } } ); diff --git a/x-pack/plugins/grokdebugger/server/shared_imports.ts b/x-pack/plugins/grokdebugger/server/shared_imports.ts new file mode 100644 index 0000000000000..068cddcee4c86 --- /dev/null +++ b/x-pack/plugins/grokdebugger/server/shared_imports.ts @@ -0,0 +1,7 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export { handleEsError } from '../../../../src/plugins/es_ui_shared/server';