diff --git a/src/components/custom-metrics/custom-metric-form.tsx b/src/components/custom-metrics/custom-metric-form.tsx index 664fdd911..bec187da1 100644 --- a/src/components/custom-metrics/custom-metric-form.tsx +++ b/src/components/custom-metrics/custom-metric-form.tsx @@ -60,6 +60,17 @@ interface CustomMetricFormProps { footerSlot?: HTMLElement | null; } +/** + * The sentence for a refused save. A name clash is read by its code: a 409 + * can also be the first save still running under the same key. + */ +function saveRefusal(err: unknown, t: (key: string) => string): string { + return err instanceof ApiError && + err.meta?.errorCode === "customMetric.name_taken" + ? t("customMetrics.form.nameTaken") + : localizedApiError(err, t, "customMetrics.form.saveError"); +} + /** * v1.25.5 — define or edit a custom metric. * @@ -132,13 +143,7 @@ export function CustomMetricForm({ ); onSuccess?.(saved); } catch (err) { - // By its code: a 409 can also be the first save still running. - setError( - err instanceof ApiError && - err.meta?.errorCode === "customMetric.name_taken" - ? t("customMetrics.form.nameTaken") - : localizedApiError(err, t, "customMetrics.form.saveError"), - ); + setError(saveRefusal(err, t)); } finally { setSubmitting(false); }