mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Improve 404 and other HTTP request error handling (#82249)
This commit is contained in:
parent
d91803c35a
commit
70fc603d26
@ -1,9 +1,10 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import { NavModelItem } from '@grafana/data';
|
import { NavModelItem } from '@grafana/data';
|
||||||
import { config } from '@grafana/runtime';
|
import { config, isFetchError } from '@grafana/runtime';
|
||||||
import { Alert, withErrorBoundary } from '@grafana/ui';
|
import { Alert, withErrorBoundary } from '@grafana/ui';
|
||||||
import { SafeDynamicImport } from 'app/core/components/DynamicImports/SafeDynamicImport';
|
import { SafeDynamicImport } from 'app/core/components/DynamicImports/SafeDynamicImport';
|
||||||
|
import { EntityNotFound } from 'app/core/components/PageNotFound/EntityNotFound';
|
||||||
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
|
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
|
||||||
|
|
||||||
import { AlertingPageWrapper } from './components/AlertingPageWrapper';
|
import { AlertingPageWrapper } from './components/AlertingPageWrapper';
|
||||||
@ -52,7 +53,7 @@ const RuleViewerV2Wrapper = (props: RuleViewerProps) => {
|
|||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
<AlertingPageWrapper pageNav={defaultPageNav} navId="alert-list">
|
<AlertingPageWrapper pageNav={defaultPageNav} navId="alert-list">
|
||||||
<Alert title={'Something went wrong loading the rule'}>{stringifyErrorLike(error)}</Alert>
|
<ErrorMessage error={error} />
|
||||||
</AlertingPageWrapper>
|
</AlertingPageWrapper>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -76,4 +77,16 @@ const RuleViewerV2Wrapper = (props: RuleViewerProps) => {
|
|||||||
return null;
|
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' });
|
export default withErrorBoundary(RuleViewer, { style: 'page' });
|
||||||
|
@ -2,7 +2,7 @@ import { sortBy } from 'lodash';
|
|||||||
|
|
||||||
import { UrlQueryMap, Labels } from '@grafana/data';
|
import { UrlQueryMap, Labels } from '@grafana/data';
|
||||||
import { GrafanaEdition } from '@grafana/data/src/types/config';
|
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 { DataSourceRef } from '@grafana/schema';
|
||||||
import { escapePathSeparators } from 'app/features/alerting/unified/utils/rule-id';
|
import { escapePathSeparators } from 'app/features/alerting/unified/utils/rule-id';
|
||||||
import { alertInstanceKey } from 'app/features/alerting/unified/utils/rules';
|
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 {
|
export function stringifyErrorLike(error: unknown): string {
|
||||||
|
const fetchError = isFetchError(error);
|
||||||
|
if (fetchError) {
|
||||||
|
return error.data.message;
|
||||||
|
}
|
||||||
|
|
||||||
return isErrorLike(error) ? error.message : String(error);
|
return isErrorLike(error) ? error.message : String(error);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user