Skip to content

Commit

Permalink
[Obs AI Assistant] Handle Axios errors correctly (#186790)
Browse files Browse the repository at this point in the history
the `kibana` function throws an axios error, and it resulted in an error
because `toJSON()` doesn't actually return a string like I expected it
to, it returns a structured object that can be converted into a string
by calling JSON.stringify().

(cherry picked from commit ca41e66)
  • Loading branch information
dgieselaar committed Jun 25, 2024
1 parent 1619041 commit 7ea8d91
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { errors } from '@elastic/elasticsearch';
import { isAxiosError } from 'axios';
import { createFunctionResponseMessage } from '../../../common/utils/create_function_response_message';

export function createServerSideFunctionResponseError({
Expand All @@ -19,15 +20,18 @@ export function createServerSideFunctionResponseError({
}) {
const isElasticsearchError = error instanceof errors.ElasticsearchClientError;

const sanitizedError: Record<string, unknown> = JSON.parse(
'toJSON' in error && typeof error.toJSON === 'function' ? error.toJSON() : JSON.stringify(error)
);
const sanitizedError: Record<string, unknown> = JSON.parse(JSON.stringify(error));

if (isElasticsearchError) {
// remove meta key which is huge and noisy
delete sanitizedError.meta;
} else if (isAxiosError(error)) {
sanitizedError.response = { message: error.response?.data?.message };
delete sanitizedError.config;
}

delete sanitizedError.stack;

return createFunctionResponseMessage({
name,
content: {
Expand Down

0 comments on commit 7ea8d91

Please sign in to comment.