Skip to content

Commit

Permalink
[8.14] [Obs AI Assistant] Handle Axios errors correctly (#186790) (#1…
Browse files Browse the repository at this point in the history
…86865)

# Backport

This will backport the following commits from `main` to `8.14`:
- [[Obs AI Assistant] Handle Axios errors correctly
(#186790)](#186790)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Dario
Gieselaar","email":"dario.gieselaar@elastic.co"},"sourceCommit":{"committedDate":"2024-06-25T06:38:22Z","message":"[Obs
AI Assistant] Handle Axios errors correctly (#186790)\n\nthe `kibana`
function throws an axios error, and it resulted in an error\r\nbecause
`toJSON()` doesn't actually return a string like I expected it\r\nto, it
returns a structured object that can be converted into a string\r\nby
calling
JSON.stringify().","sha":"ca41e6685ea245ea8d7fb3f39b31fa18f5134821","branchLabelMapping":{"^v8.15.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:Obs
AI
Assistant","ci:project-deploy-observability","v8.15.0","v8.14.2"],"title":"[Obs
AI Assistant] Handle Axios errors
correctly","number":186790,"url":"#186790
AI Assistant] Handle Axios errors correctly (#186790)\n\nthe `kibana`
function throws an axios error, and it resulted in an error\r\nbecause
`toJSON()` doesn't actually return a string like I expected it\r\nto, it
returns a structured object that can be converted into a string\r\nby
calling
JSON.stringify().","sha":"ca41e6685ea245ea8d7fb3f39b31fa18f5134821"}},"sourceBranch":"main","suggestedTargetBranches":["8.14"],"targetPullRequestStates":[{"branch":"main","label":"v8.15.0","branchLabelMappingKey":"^v8.15.0$","isSourceBranch":true,"state":"MERGED","url":"#186790
AI Assistant] Handle Axios errors correctly (#186790)\n\nthe `kibana`
function throws an axios error, and it resulted in an error\r\nbecause
`toJSON()` doesn't actually return a string like I expected it\r\nto, it
returns a structured object that can be converted into a string\r\nby
calling
JSON.stringify().","sha":"ca41e6685ea245ea8d7fb3f39b31fa18f5134821"}},{"branch":"8.14","label":"v8.14.2","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Dario Gieselaar <dario.gieselaar@elastic.co>
  • Loading branch information
kibanamachine and dgieselaar committed Jun 25, 2024
1 parent 1619041 commit 12da161
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 12da161

Please sign in to comment.