Skip to content

Commit

Permalink
[Search] Log and return instead of swallow errors in Playground (#190796
Browse files Browse the repository at this point in the history
)

## Summary

This stops the Kibana server from swallowing all playground errors,
instead logging them and then returning a meaningful error to the
frontend.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
sphilipse and kibanamachine authored Aug 21, 2024
1 parent b50eb08 commit 8d4704f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
10 changes: 5 additions & 5 deletions x-pack/plugins/search_playground/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function defineRoutes({
}),
},
},
errorHandler(async (context, request, response) => {
errorHandler(logger)(async (context, request, response) => {
const { client } = (await context.core).elasticsearch;

const { indices } = request.body;
Expand Down Expand Up @@ -89,14 +89,14 @@ export function defineRoutes({
}),
},
},
errorHandler(async (context, request, response) => {
errorHandler(logger)(async (context, request, response) => {
const [{ analytics }, { actions, cloud }] = await getStartServices();

const { client } = (await context.core).elasticsearch;
const aiClient = Assist({
es_client: client.asCurrentUser,
} as AssistClientOptionsWithClient);
const { messages, data } = await request.body;
const { messages, data } = request.body;
const { chatModel, chatPrompt, questionRewritePrompt, connector } = await getChatParams(
{
connectorId: data.connector_id,
Expand Down Expand Up @@ -184,7 +184,7 @@ export function defineRoutes({
}),
},
},
errorHandler(async (context, request, response) => {
errorHandler(logger)(async (context, request, response) => {
const { name, expiresInDays, indices } = request.body;
const { client } = (await context.core).elasticsearch;

Expand Down Expand Up @@ -223,7 +223,7 @@ export function defineRoutes({
}),
},
},
errorHandler(async (context, request, response) => {
errorHandler(logger)(async (context, request, response) => {
const { search_query: searchQuery, size } = request.query;
const {
client: { asCurrentUser },
Expand Down
12 changes: 11 additions & 1 deletion x-pack/plugins/search_playground/server/utils/error_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
*/

import { RequestHandlerWrapper } from '@kbn/core-http-server';
import { KibanaServerError } from '@kbn/kibana-utils-plugin/common';
import type { Logger } from '@kbn/logging';

export const errorHandler: RequestHandlerWrapper = (handler) => {
function isKibanaServerError(error: any): error is KibanaServerError {
return error.statusCode && error.message;
}

export const errorHandler: (logger: Logger) => RequestHandlerWrapper = (logger) => (handler) => {
return async (context, request, response) => {
try {
return await handler(context, request, response);
} catch (e) {
logger.error(e);
if (isKibanaServerError(e)) {
return response.customError({ statusCode: e.statusCode, body: e.message });
}
throw e;
}
};
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/search_playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"@kbn/analytics",
"@kbn/usage-collection-plugin",
"@kbn/console-plugin",
"@kbn/utility-types"
"@kbn/utility-types",
"@kbn/kibana-utils-plugin"
],
"exclude": [
"target/**/*",
Expand Down

0 comments on commit 8d4704f

Please sign in to comment.