From 89558f20bd5c827b6ceb17c730499ad077a57de0 Mon Sep 17 00:00:00 2001 From: Domas Date: Wed, 19 May 2021 10:12:44 +0300 Subject: [PATCH] Alerting: improve error presentation in forms (#34309) --- .../features/alerting/unified/AmRoutes.tsx | 5 - .../alerting/unified/api/alertmanager.ts | 13 +- .../features/alerting/unified/api/ruler.ts | 15 +- .../receivers/form/ReceiverForm.tsx | 16 +- .../components/rule-editor/AlertRuleForm.tsx | 34 ++-- .../unified/components/rules/RulesFilter.tsx | 1 + .../components/silences/SilencesEditor.tsx | 9 +- .../alerting/unified/state/actions.ts | 146 ++++++++++-------- .../features/alerting/unified/utils/redux.ts | 28 +++- 9 files changed, 148 insertions(+), 119 deletions(-) diff --git a/public/app/features/alerting/unified/AmRoutes.tsx b/public/app/features/alerting/unified/AmRoutes.tsx index 1a5e2fa7198..55b6f3ef906 100644 --- a/public/app/features/alerting/unified/AmRoutes.tsx +++ b/public/app/features/alerting/unified/AmRoutes.tsx @@ -97,11 +97,6 @@ const AmRoutes: FC = () => { return ( - {savingError && !saving && ( - - {savingError.message || 'Unknown error.'} - - )} {resultError && !resultLoading && ( {resultError.message || 'Unknown error.'} diff --git a/public/app/features/alerting/unified/api/alertmanager.ts b/public/app/features/alerting/unified/api/alertmanager.ts index 35be36a3e7e..dff5f43f1e5 100644 --- a/public/app/features/alerting/unified/api/alertmanager.ts +++ b/public/app/features/alerting/unified/api/alertmanager.ts @@ -71,10 +71,15 @@ export async function createOrUpdateSilence( alertmanagerSourceName: string, payload: SilenceCreatePayload ): Promise { - const result = await getBackendSrv().post( - `/api/alertmanager/${getDatasourceAPIId(alertmanagerSourceName)}/api/v2/silences`, - payload - ); + const result = await getBackendSrv() + .fetch({ + url: `/api/alertmanager/${getDatasourceAPIId(alertmanagerSourceName)}/api/v2/silences`, + data: payload, + showErrorAlert: false, + showSuccessAlert: false, + method: 'POST', + }) + .toPromise(); return result.data; } diff --git a/public/app/features/alerting/unified/api/ruler.ts b/public/app/features/alerting/unified/api/ruler.ts index c3a515df9dd..f73440025ff 100644 --- a/public/app/features/alerting/unified/api/ruler.ts +++ b/public/app/features/alerting/unified/api/ruler.ts @@ -51,11 +51,16 @@ export async function fetchRulerRulesGroup( } export async function deleteRulerRulesGroup(dataSourceName: string, namespace: string, groupName: string) { - return getBackendSrv().delete( - `/api/ruler/${getDatasourceAPIId(dataSourceName)}/api/v1/rules/${encodeURIComponent( - namespace - )}/${encodeURIComponent(groupName)}` - ); + return getBackendSrv() + .fetch({ + url: `/api/ruler/${getDatasourceAPIId(dataSourceName)}/api/v1/rules/${encodeURIComponent( + namespace + )}/${encodeURIComponent(groupName)}`, + method: 'DELETE', + showSuccessAlert: false, + showErrorAlert: false, + }) + .toPromise(); } // false in case ruler is not supported. this is weird, but we'll work on it diff --git a/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx b/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx index ac2d3854105..d7dc4a7606a 100644 --- a/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx +++ b/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx @@ -1,5 +1,5 @@ import { css } from '@emotion/css'; -import { GrafanaTheme2 } from '@grafana/data'; +import { GrafanaTheme2, AppEvents } from '@grafana/data'; import { Alert, Button, Field, Input, LinkButton, useStyles2 } from '@grafana/ui'; import { useCleanup } from 'app/core/hooks/useCleanup'; import { AlertManagerCortexConfig } from 'app/plugins/datasource/alertmanager/types'; @@ -12,6 +12,7 @@ import { ChannelValues, CommonSettingsComponentType, ReceiverFormValues } from ' import { makeAMLink } from '../../../utils/misc'; import { ChannelSubForm } from './ChannelSubForm'; import { DeletedSubForm } from './fields/DeletedSubform'; +import { appEvents } from 'app/core/core'; interface Props { config: AlertManagerCortexConfig; @@ -53,7 +54,7 @@ export function ReceiverForm({ useCleanup((state) => state.unifiedAlerting.saveAMConfig); - const { loading, error } = useUnifiedAlertingSelector((state) => state.saveAMConfig); + const { loading } = useUnifiedAlertingSelector((state) => state.saveAMConfig); const { handleSubmit, @@ -79,6 +80,10 @@ export function ReceiverForm({ }); }; + const onInvalid = () => { + appEvents.emit(AppEvents.alertError, ['There are errors in the form. Please correct them and try again!']); + }; + return ( {!config.alertmanager_config.route && ( @@ -86,13 +91,8 @@ export function ReceiverForm({ Because there is no default policy configured yet, this contact point will automatically be set as default. )} -
+

{initialValues ? 'Update contact point' : 'Create contact point'}

- {error && ( - - {error.message || String(error)} - - )} = ({ existing }) => { const formAPI = useForm({ mode: 'onSubmit', defaultValues, + shouldFocusError: true, }); - const { - handleSubmit, - watch, - formState: { errors }, - } = formAPI; - - const hasErrors = !!Object.values(errors).filter((x) => !!x).length; + const { handleSubmit, watch } = formAPI; const type = watch('type'); const dataSourceName = watch('dataSourceName'); @@ -78,6 +75,10 @@ export const AlertRuleForm: FC = ({ existing }) => { ); }; + const onInvalid = () => { + appEvents.emit(AppEvents.alertError, ['There are errors in the form. Please correct them and try again!']); + }; + return ( e.preventDefault()} className={styles.form}> @@ -90,7 +91,7 @@ export const AlertRuleForm: FC = ({ existing }) => {