feat(web): mutations throw HttpError(status) so failures are status-aware (#63)
This commit is contained in:
+18
-18
@@ -179,9 +179,9 @@ export function useCreateObject() {
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (body: ObjectCreateRequest) => {
|
||||
const { data, error } = await api.POST("/api/admin/objects", { body });
|
||||
const { data, error, response } = await api.POST("/api/admin/objects", { body });
|
||||
|
||||
if (error || !data) throw new Error("create failed");
|
||||
if (error || !data) throw new HttpError(response.status);
|
||||
|
||||
return data;
|
||||
},
|
||||
@@ -200,7 +200,7 @@ export function useUpdateObject() {
|
||||
body,
|
||||
});
|
||||
|
||||
if (response.status !== 204) throw new Error("update failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: (_d, { id }) => {
|
||||
void qc.invalidateQueries({ queryKey: ["objects"] });
|
||||
@@ -227,7 +227,7 @@ export function useSetFields() {
|
||||
throw new FieldRejection(detail.field, detail.code);
|
||||
}
|
||||
|
||||
throw new Error("set fields failed");
|
||||
throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: (_d, { id }) => {
|
||||
void qc.invalidateQueries({ queryKey: ["object", id] });
|
||||
@@ -245,7 +245,7 @@ export function useDeleteObject() {
|
||||
params: { path: { id } },
|
||||
});
|
||||
|
||||
if (response.status !== 204) throw new Error("delete failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["objects"] }),
|
||||
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
|
||||
@@ -274,9 +274,9 @@ export function useCreateVocabulary() {
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (body: NewVocabularyRequest) => {
|
||||
const { data, error } = await api.POST("/api/admin/vocabularies", { body });
|
||||
const { data, error, response } = await api.POST("/api/admin/vocabularies", { body });
|
||||
|
||||
if (error || !data) throw new Error("create vocabulary failed");
|
||||
if (error || !data) throw new HttpError(response.status);
|
||||
|
||||
return data;
|
||||
},
|
||||
@@ -303,7 +303,7 @@ export function useAddTerm() {
|
||||
body: { external_uri, labels },
|
||||
});
|
||||
|
||||
if (response.status !== 201) throw new Error("add term failed");
|
||||
if (response.status !== 201) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: (_result, { vocabularyId }) =>
|
||||
qc.invalidateQueries({ queryKey: ["terms", vocabularyId] }),
|
||||
@@ -328,7 +328,7 @@ export function useCreateAuthority() {
|
||||
body: { kind, external_uri, labels },
|
||||
});
|
||||
|
||||
if (response.status !== 201) throw new Error("create authority failed");
|
||||
if (response.status !== 201) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: (_result, { kind }) =>
|
||||
qc.invalidateQueries({ queryKey: ["authorities", kind] }),
|
||||
@@ -379,7 +379,7 @@ export function useCreateFieldDefinition() {
|
||||
mutationFn: async (body: NewFieldDefinitionRequest) => {
|
||||
const { data, response } = await api.POST("/api/admin/field-definitions", { body });
|
||||
|
||||
if (response.status !== 201 || !data) throw new Error("failed to create field definition");
|
||||
if (response.status !== 201 || !data) throw new HttpError(response.status);
|
||||
|
||||
return data;
|
||||
},
|
||||
@@ -438,7 +438,7 @@ export function useUpdateTerm() {
|
||||
body: { external_uri, labels },
|
||||
});
|
||||
|
||||
if (response.status !== 204) throw new Error("update term failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: (_d, { vocabularyId }) => qc.invalidateQueries({ queryKey: ["terms", vocabularyId] }),
|
||||
meta: { successMessage: "toast.saved" },
|
||||
@@ -455,7 +455,7 @@ export function useDeleteTerm() {
|
||||
});
|
||||
|
||||
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
|
||||
if (response.status !== 204) throw new Error("delete term failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: (_d, { vocabularyId }) => qc.invalidateQueries({ queryKey: ["terms", vocabularyId] }),
|
||||
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
|
||||
@@ -472,7 +472,7 @@ export function useRenameVocabulary() {
|
||||
body: { key },
|
||||
});
|
||||
|
||||
if (response.status !== 204) throw new Error("rename failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["vocabularies"] }),
|
||||
meta: { successMessage: "toast.renamed", suppressErrorToast: true },
|
||||
@@ -489,7 +489,7 @@ export function useDeleteVocabulary() {
|
||||
});
|
||||
|
||||
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
|
||||
if (response.status !== 204) throw new Error("delete vocabulary failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["vocabularies"] }),
|
||||
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
|
||||
@@ -515,7 +515,7 @@ export function useUpdateAuthority() {
|
||||
body: { external_uri, labels },
|
||||
});
|
||||
|
||||
if (response.status !== 204) throw new Error("update authority failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: (_d, { kind }) => qc.invalidateQueries({ queryKey: ["authorities", kind] }),
|
||||
meta: { successMessage: "toast.saved" },
|
||||
@@ -532,7 +532,7 @@ export function useDeleteAuthority() {
|
||||
});
|
||||
|
||||
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
|
||||
if (response.status !== 204) throw new Error("delete authority failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: (_d, { kind }) => qc.invalidateQueries({ queryKey: ["authorities", kind] }),
|
||||
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
|
||||
@@ -559,7 +559,7 @@ export function useUpdateFieldDefinition() {
|
||||
body: { required, group, labels },
|
||||
});
|
||||
|
||||
if (response.status !== 204) throw new Error("update field failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["field-definitions"] }),
|
||||
meta: { successMessage: "toast.saved", suppressErrorToast: true },
|
||||
@@ -576,7 +576,7 @@ export function useDeleteFieldDefinition() {
|
||||
});
|
||||
|
||||
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
|
||||
if (response.status !== 204) throw new Error("delete field failed");
|
||||
if (response.status !== 204) throw new HttpError(response.status);
|
||||
},
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["field-definitions"] }),
|
||||
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
|
||||
|
||||
Reference in New Issue
Block a user