From 90d6f73cf58aebed9f4aa63951081f2ac42743ec Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Sat, 21 Sep 2024 02:43:04 +1000 Subject: [PATCH] [8.x] [Inventory][ECO] API error handler (#193560) (#193606) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Backport This will backport the following commits from `main` to `8.x`: - [[Inventory][ECO] API error handler (#193560)](https://github.com/elastic/kibana/pull/193560) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: CauĂȘ Marcondes <55978943+cauemarcondes@users.noreply.github.com> --- .../hooks/use_inventory_abortable_async.ts | 36 +++++++++++++++++++ .../public/pages/inventory_page/index.tsx | 8 ++--- .../inventory/tsconfig.json | 3 +- 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 x-pack/plugins/observability_solution/inventory/public/hooks/use_inventory_abortable_async.ts diff --git a/x-pack/plugins/observability_solution/inventory/public/hooks/use_inventory_abortable_async.ts b/x-pack/plugins/observability_solution/inventory/public/hooks/use_inventory_abortable_async.ts new file mode 100644 index 0000000000000..60b2fca72e721 --- /dev/null +++ b/x-pack/plugins/observability_solution/inventory/public/hooks/use_inventory_abortable_async.ts @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import { useAbortableAsync } from '@kbn/observability-utils/hooks/use_abortable_async'; +import { i18n } from '@kbn/i18n'; +import { IHttpFetchError, ResponseErrorBody } from '@kbn/core-http-browser'; +import { useKibana } from './use_kibana'; + +const getDetailsFromErrorResponse = (error: IHttpFetchError) => + error.body?.message ?? error.response?.statusText; + +export function useInventoryAbortableAsync(...args: Parameters>) { + const { + core: { notifications }, + } = useKibana(); + const response = useAbortableAsync(...args); + + if (response.error) { + const errorMessage = + 'response' in response.error + ? getDetailsFromErrorResponse(response.error as IHttpFetchError) + : response.error.message; + + notifications.toasts.addDanger({ + title: i18n.translate('xpack.inventory.apiCall.error.title', { + defaultMessage: `Error while fetching resource`, + }), + text: errorMessage, + }); + } + + return response; +} diff --git a/x-pack/plugins/observability_solution/inventory/public/pages/inventory_page/index.tsx b/x-pack/plugins/observability_solution/inventory/public/pages/inventory_page/index.tsx index e77b46b26dc79..be54ff531ca44 100644 --- a/x-pack/plugins/observability_solution/inventory/public/pages/inventory_page/index.tsx +++ b/x-pack/plugins/observability_solution/inventory/public/pages/inventory_page/index.tsx @@ -4,13 +4,13 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import React from 'react'; -import { useAbortableAsync } from '@kbn/observability-utils/hooks/use_abortable_async'; import { EuiDataGridSorting } from '@elastic/eui'; +import React from 'react'; import { EntitiesGrid } from '../../components/entities_grid'; -import { useKibana } from '../../hooks/use_kibana'; +import { useInventoryAbortableAsync } from '../../hooks/use_inventory_abortable_async'; import { useInventoryParams } from '../../hooks/use_inventory_params'; import { useInventoryRouter } from '../../hooks/use_inventory_router'; +import { useKibana } from '../../hooks/use_kibana'; export function InventoryPage() { const { @@ -20,7 +20,7 @@ export function InventoryPage() { const { sortDirection, sortField, pageIndex } = query; const inventoryRoute = useInventoryRouter(); - const { value = { entities: [] }, loading } = useAbortableAsync( + const { value = { entities: [] }, loading } = useInventoryAbortableAsync( ({ signal }) => { return inventoryAPIClient.fetch('GET /internal/inventory/entities', { params: { diff --git a/x-pack/plugins/observability_solution/inventory/tsconfig.json b/x-pack/plugins/observability_solution/inventory/tsconfig.json index e5e530ce1233f..5c17e404701d5 100644 --- a/x-pack/plugins/observability_solution/inventory/tsconfig.json +++ b/x-pack/plugins/observability_solution/inventory/tsconfig.json @@ -37,6 +37,7 @@ "@kbn/es-types", "@kbn/entities-schema", "@kbn/i18n-react", - "@kbn/io-ts-utils" + "@kbn/io-ts-utils", + "@kbn/core-http-browser" ] }