Skip to content

Commit

Permalink
error
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Sep 20, 2024
1 parent 1290004 commit 31bf014
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@
*/
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<ResponseErrorBody>) =>
error.body?.message ?? error.response?.statusText;

export function useInventoryAbortableAsync<T>(...args: Parameters<typeof useAbortableAsync<T>>) {
const {
core: { notifications },
} = useKibana();
const response = useAbortableAsync(...args);

if (response.error) {
const errorMessage =
'response' in response.error
? getDetailsFromErrorResponse(response.error as IHttpFetchError<ResponseErrorBody>)
: response.error.message;

notifications.toasts.addDanger({
title: i18n.translate('xpack.inventory.apiCall.error.title', {
defaultMessage: `Error while fetching resource`,
}),
text: response.error.message,
text: errorMessage,
});
}

Expand Down

0 comments on commit 31bf014

Please sign in to comment.