Skip to content

Commit

Permalink
[Grokdebugger] Fix simulate error handling (#83036)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jloleysens committed Nov 11, 2020
1 parent c32215d commit 9038e5c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class GrokdebuggerService {
return GrokdebuggerResponse.fromUpstreamJSON(response);
})
.catch((e) => {
throw e.body.message;
throw new Error(e.body.message);
});
}
}
26 changes: 1 addition & 25 deletions x-pack/plugins/grokdebugger/server/lib/kibana_framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
*/

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(),
// We don't know these key / values up front as they depend on user input
customPatterns: schema.object({}, { unknowns: 'allow' }),
});

export function registerGrokSimulateRoute(framework) {
export function registerGrokSimulateRoute(framework: KibanaFramework) {
framework.registerRoute(
{
method: 'post',
Expand All @@ -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 });
}
}
);
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/grokdebugger/server/shared_imports.ts
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit 9038e5c

Please sign in to comment.