Alerting: Improve 404 and other HTTP request error handling (#82249)

This commit is contained in:
Gilles De Mey 2024-02-12 12:17:19 +01:00 committed by GitHub
parent d91803c35a
commit 70fc603d26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

View File

@ -1,9 +1,10 @@
import React from 'react';
import { NavModelItem } from '@grafana/data';
import { config } from '@grafana/runtime';
import { config, isFetchError } from '@grafana/runtime';
import { Alert, withErrorBoundary } from '@grafana/ui';
import { SafeDynamicImport } from 'app/core/components/DynamicImports/SafeDynamicImport';
import { EntityNotFound } from 'app/core/components/PageNotFound/EntityNotFound';
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
import { AlertingPageWrapper } from './components/AlertingPageWrapper';
@ -52,7 +53,7 @@ const RuleViewerV2Wrapper = (props: RuleViewerProps) => {
if (error) {
return (
<AlertingPageWrapper pageNav={defaultPageNav} navId="alert-list">
<Alert title={'Something went wrong loading the rule'}>{stringifyErrorLike(error)}</Alert>
<ErrorMessage error={error} />
</AlertingPageWrapper>
);
}
@ -76,4 +77,16 @@ const RuleViewerV2Wrapper = (props: RuleViewerProps) => {
return null;
};
interface ErrorMessageProps {
error: unknown;
}
function ErrorMessage({ error }: ErrorMessageProps) {
if (isFetchError(error) && error.status === 404) {
return <EntityNotFound entity="Rule" />;
}
return <Alert title={'Something went wrong loading the rule'}>{stringifyErrorLike(error)}</Alert>;
}
export default withErrorBoundary(RuleViewer, { style: 'page' });

View File

@ -2,7 +2,7 @@ import { sortBy } from 'lodash';
import { UrlQueryMap, Labels } from '@grafana/data';
import { GrafanaEdition } from '@grafana/data/src/types/config';
import { config } from '@grafana/runtime';
import { config, isFetchError } from '@grafana/runtime';
import { DataSourceRef } from '@grafana/schema';
import { escapePathSeparators } from 'app/features/alerting/unified/utils/rule-id';
import { alertInstanceKey } from 'app/features/alerting/unified/utils/rules';
@ -231,5 +231,10 @@ export function isErrorLike(error: unknown): error is Error {
}
export function stringifyErrorLike(error: unknown): string {
const fetchError = isFetchError(error);
if (fetchError) {
return error.data.message;
}
return isErrorLike(error) ? error.message : String(error);
}