[Grokdebugger] Fix simulate error handling (#83036)

* 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
This commit is contained in:
Jean-Louis Leysens 2020-11-11 15:43:17 +01:00 committed by GitHub
parent c32215d7ff
commit 9038e5c397
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 34 deletions

View file

@ -21,7 +21,7 @@ export class GrokdebuggerService {
return GrokdebuggerResponse.fromUpstreamJSON(response);
})
.catch((e) => {
throw e.body.message;
throw new Error(e.body.message);
});
}
}

View file

@ -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<any>;
public async callWithRequest(
requestContext: RequestHandlerContext,
endpoint: string,
options?: any
) {
const { elasticsearch } = requestContext.core;
return elasticsearch.legacy.client.callAsCurrentUser(endpoint, options);
}
}

View file

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

View file

@ -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';