Skip to content

Commit

Permalink
Return error from RQ useFetchList hook, have better error handling (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CoenWarmer authored May 11, 2023
1 parent f9f4c1a commit dcf1c23
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useQuery,
useQueryClient,
} from '@tanstack/react-query';
import { i18n } from '@kbn/i18n';
import { FindSLOResponse } from '@kbn/slo-schema';

import { useKibana } from '../../utils/kibana_react';
Expand Down Expand Up @@ -47,10 +48,15 @@ export function useFetchSloList({
indicatorTypes = [],
shouldRefetch,
}: SLOListParams | undefined = {}): UseFetchSloListResponse {
const { http } = useKibana().services;
const {
http,
notifications: { toasts },
} = useKibana().services;
const queryClient = useQueryClient();

const [stateRefetchInterval, setStateRefetchInterval] = useState(SHORT_REFETCH_INTERVAL);
const [stateRefetchInterval, setStateRefetchInterval] = useState<number | undefined>(
SHORT_REFETCH_INTERVAL
);

const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data, refetch } = useQuery(
{
Expand All @@ -72,13 +78,19 @@ export function useFetchSloList({

return response;
} catch (error) {
// ignore error
throw error;
}
},
keepPreviousData: true,
refetchOnWindowFocus: false,
refetchInterval: shouldRefetch ? stateRefetchInterval : undefined,
staleTime: 1000,
retry: (failureCount, error) => {
if (String(error) === 'Error: Forbidden') {
return false;
}
return failureCount < 4;
},
onSuccess: ({ results }: FindSLOResponse) => {
if (!shouldRefetch) {
return;
Expand All @@ -102,6 +114,13 @@ export function useFetchSloList({
exact: false,
});
},
onError: (error: Error) => {
toasts.addError(error, {
title: i18n.translate('xpack.observability.slo.list.errorNotification', {
defaultMessage: 'Something went wrong while fetching SLOs',
}),
});
},
}
);

Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/observability/public/pages/slos/slos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function SlosPage() {
const { hasWriteCapabilities } = useCapabilities();
const { hasAtLeast } = useLicense();

const { isInitialLoading, isLoading, sloList } = useFetchSloList();
const { isInitialLoading, isLoading, isError, sloList } = useFetchSloList();

const { total } = sloList || {};

Expand All @@ -46,6 +46,12 @@ export function SlosPage() {
},
]);

useEffect(() => {
if ((!isLoading && total === 0) || hasAtLeast('platinum') === false || isError) {
navigateToUrl(basePath.prepend(paths.observability.slosWelcome));
}
}, [basePath, hasAtLeast, isError, isLoading, navigateToUrl, total]);

const handleClickCreateSlo = () => {
navigateToUrl(basePath.prepend(paths.observability.sloCreate));
};
Expand All @@ -54,12 +60,6 @@ export function SlosPage() {
setIsAutoRefreshing(!isAutoRefreshing);
};

useEffect(() => {
if ((!isLoading && total === 0) || hasAtLeast('platinum') === false) {
navigateToUrl(basePath.prepend(paths.observability.slosWelcome));
}
}, [basePath, hasAtLeast, isLoading, navigateToUrl, total]);

if (isInitialLoading) {
return null;
}
Expand Down

0 comments on commit dcf1c23

Please sign in to comment.