fix(services): surface validation errors in add-service dialog
CreateServiceDialog.save() awaited mutateAsync without a try/catch, so a backend 422 (e.g. base_url missing http:// schema) threw uncaught and the dialog sat silent with no feedback. Wrap in try/catch, hold the error in local state, render a destructive Alert above the footer. Reset/onClose only on success; on error the user can fix and retry.
This commit is contained in:
@@ -175,9 +175,11 @@ function CreateServiceDialog({
|
|||||||
const { data: types = [] } = useServiceTypes();
|
const { data: types = [] } = useServiceTypes();
|
||||||
const saveService = useSaveServiceInstance();
|
const saveService = useSaveServiceInstance();
|
||||||
const [draft, setDraft] = useState<CreateDraft | null>(null);
|
const [draft, setDraft] = useState<CreateDraft | null>(null);
|
||||||
|
const [submitError, setSubmitError] = useState<string | null>(null);
|
||||||
|
|
||||||
function reset() {
|
function reset() {
|
||||||
setDraft(null);
|
setDraft(null);
|
||||||
|
setSubmitError(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function save() {
|
async function save() {
|
||||||
@@ -190,9 +192,14 @@ function CreateServiceDialog({
|
|||||||
secrets: draft.secrets,
|
secrets: draft.secrets,
|
||||||
enabled: draft.enabled,
|
enabled: draft.enabled,
|
||||||
};
|
};
|
||||||
await saveService.mutateAsync(input);
|
setSubmitError(null);
|
||||||
reset();
|
try {
|
||||||
|
await saveService.mutateAsync(input);
|
||||||
|
reset();
|
||||||
onClose();
|
onClose();
|
||||||
|
} catch (err) {
|
||||||
|
setSubmitError(err instanceof Error ? err.message : String(err));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedType = types.find((t) => t.service_type === draft?.serviceType);
|
const selectedType = types.find((t) => t.service_type === draft?.serviceType);
|
||||||
@@ -264,6 +271,11 @@ function CreateServiceDialog({
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
{submitError ? (
|
||||||
|
<Alert variant="destructive">
|
||||||
|
<AlertDescription>{submitError}</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
) : null}
|
||||||
{draft ? (
|
{draft ? (
|
||||||
<DialogFooter
|
<DialogFooter
|
||||||
onCancel={reset}
|
onCancel={reset}
|
||||||
|
|||||||
Reference in New Issue
Block a user