Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Grokdebugger] Fix simulate error handling #83036

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';