fix(groups): show API error messages on create and update

保存分组失败时显示标准化 API 错误信息。
This commit is contained in:
Brisbanehuang
2026-08-29 17:28:20 +08:00
committed by GitHub
parent c31fe2ed9e
commit 706b5676a6
2 changed files with 36 additions and 4 deletions
+2 -2
View File
@@ -6087,7 +6087,7 @@ const handleCreateGroup = async () => {
}
} catch (error: any) {
appStore.showError(
error.response?.data?.detail || t("admin.groups.failedToCreate"),
extractApiErrorMessage(error, t("admin.groups.failedToCreate")),
);
console.error("Error creating group:", error);
// Don't advance tour on error
@@ -6358,7 +6358,7 @@ const handleUpdateGroup = async () => {
loadGroups();
} catch (error: any) {
appStore.showError(
error.response?.data?.detail || t("admin.groups.failedToUpdate"),
extractApiErrorMessage(error, t("admin.groups.failedToUpdate")),
);
console.error("Error updating group:", error);
} finally {
@@ -8,6 +8,7 @@ import GroupsView from '@/views/admin/GroupsView.vue'
const {
listGroups,
duplicateGroup,
updateGroup,
getModelsListCandidates,
getUsageSummary,
getCapacitySummary,
@@ -17,6 +18,7 @@ const {
} = vi.hoisted(() => ({
listGroups: vi.fn(),
duplicateGroup: vi.fn(),
updateGroup: vi.fn(),
getModelsListCandidates: vi.fn(),
getUsageSummary: vi.fn(),
getCapacitySummary: vi.fn(),
@@ -36,7 +38,7 @@ vi.mock('@/api/admin', () => ({
getLiveCapability,
getAll: vi.fn(),
create: vi.fn(),
update: vi.fn(),
update: updateGroup,
delete: vi.fn(),
updateSortOrder: vi.fn()
},
@@ -136,6 +138,13 @@ const DataTableStub = defineComponent({
template: '<div><div v-for="row in data" :key="row.id"><slot name="cell-actions" :row="row" /></div></div>'
})
const BaseDialogStub = defineComponent({
props: {
show: { type: Boolean, default: false }
},
template: '<div v-if="show"><slot /><slot name="footer" /></div>'
})
function mountView() {
return mount(GroupsView, {
global: {
@@ -144,7 +153,7 @@ function mountView() {
TablePageLayout: TablePageLayoutStub,
DataTable: DataTableStub,
Pagination: true,
BaseDialog: true,
BaseDialog: BaseDialogStub,
ConfirmDialog: true,
EmptyState: true,
Select: true,
@@ -166,6 +175,7 @@ describe('GroupsView duplicate action', () => {
for (const fn of [
listGroups,
duplicateGroup,
updateGroup,
getModelsListCandidates,
getUsageSummary,
getCapacitySummary,
@@ -270,4 +280,26 @@ describe('GroupsView duplicate action', () => {
expect(showError).not.toHaveBeenCalledWith('admin.groups.duplicateFailed')
wrapper.unmount()
})
it('shows the standardized API message when updating a group fails', async () => {
updateGroup.mockRejectedValueOnce({
status: 409,
code: 409,
message: 'group name already exists',
reason: 'GROUP_EXISTS'
})
const wrapper = mountView()
await flushPromises()
const editButton = wrapper.findAll('button').find((button) => button.text() === 'common.edit')
expect(editButton).toBeTruthy()
await editButton!.trigger('click')
await flushPromises()
await wrapper.get('#edit-group-form').trigger('submit')
await flushPromises()
expect(updateGroup).toHaveBeenCalledTimes(1)
expect(showError).toHaveBeenCalledWith('group name already exists')
wrapper.unmount()
})
})