Skip to content

Commit

Permalink
fix: fix request not displaying interface error messages correctly (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb authored Oct 9, 2024
1 parent 8fe87b1 commit ba46625
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions apps/backend-mock/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export function useResponseError(message: string, error: any = null) {

export function forbiddenResponse(event: H3Event<EventHandlerRequest>) {
setResponseStatus(event, 403);
return useResponseError('ForbiddenException', 'Forbidden Exception');
return useResponseError('Forbidden Exception', 'Forbidden Exception');
}

export function unAuthorizedResponse(event: H3Event<EventHandlerRequest>) {
setResponseStatus(event, 401);
return useResponseError('UnauthorizedException', 'Unauthorized Exception');
return useResponseError('Unauthorized Exception', 'Unauthorized Exception');
}

export function sleep(ms: number) {
Expand Down
8 changes: 6 additions & 2 deletions apps/web-antd/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ function createRequestClient(baseURL: string) {

// 通用的错误处理,如果没有进入上面的错误处理逻辑,就会进入这里
client.addResponseInterceptor(
errorMessageResponseInterceptor((msg: string, _error) => {
errorMessageResponseInterceptor((msg: string, error) => {
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
message.error(msg);
// 当前mock接口返回的错误字段是 error 或者 message
const responseData = error?.response?.data ?? {};
const errorMessage = responseData?.error ?? responseData?.message ?? '';
// 如果没有错误信息,则会根据状态码进行提示
message.error(errorMessage || msg);
}),
);

Expand Down
8 changes: 6 additions & 2 deletions apps/web-ele/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ function createRequestClient(baseURL: string) {

// 通用的错误处理,如果没有进入上面的错误处理逻辑,就会进入这里
client.addResponseInterceptor(
errorMessageResponseInterceptor((msg: string, _error) => {
errorMessageResponseInterceptor((msg: string, error) => {
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
ElMessage.error(msg);
// 当前mock接口返回的错误字段是 error 或者 message
const responseData = error?.response?.data ?? {};
const errorMessage = responseData?.error ?? responseData?.message ?? '';
// 如果没有错误信息,则会根据状态码进行提示
ElMessage.error(errorMessage || msg);
}),
);

Expand Down
8 changes: 6 additions & 2 deletions apps/web-naive/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ function createRequestClient(baseURL: string) {

// 通用的错误处理,如果没有进入上面的错误处理逻辑,就会进入这里
client.addResponseInterceptor(
errorMessageResponseInterceptor((msg: string, _error) => {
errorMessageResponseInterceptor((msg: string, error) => {
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
message.error(msg);
// 当前mock接口返回的错误字段是 error 或者 message
const responseData = error?.response?.data ?? {};
const errorMessage = responseData?.error ?? responseData?.message ?? '';
// 如果没有错误信息,则会根据状态码进行提示
message.error(errorMessage || msg);
}),
);

Expand Down
8 changes: 6 additions & 2 deletions docs/src/en/guide/essentials/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,13 @@ function createRequestClient(baseURL: string) {

// Generic error handling; if none of the above error handling logic is triggered, it will fall back to this.
client.addResponseInterceptor(
errorMessageResponseInterceptor((msg: string, _error) => {
errorMessageResponseInterceptor((msg: string, error) => {
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
message.error(msg);
// 当前mock接口返回的错误字段是 error 或者 message
const responseData = error?.response?.data ?? {};
const errorMessage = responseData?.error ?? responseData?.message ?? '';
// 如果没有错误信息,则会根据状态码进行提示
message.error(errorMessage || msg);
}),
);

Expand Down
8 changes: 6 additions & 2 deletions docs/src/guide/essentials/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,13 @@ function createRequestClient(baseURL: string) {

// 通用的错误处理,如果没有进入上面的错误处理逻辑,就会进入这里
client.addResponseInterceptor(
errorMessageResponseInterceptor((msg: string, _error) => {
errorMessageResponseInterceptor((msg: string, error) => {
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
message.error(msg);
// 当前mock接口返回的错误字段是 error 或者 message
const responseData = error?.response?.data ?? {};
const errorMessage = responseData?.error ?? responseData?.message ?? '';
// 如果没有错误信息,则会根据状态码进行提示
message.error(errorMessage || msg);
}),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const errorMessageResponseInterceptor = (
return Promise.reject(error);
}

let errorMessage = error?.response?.data?.error?.message ?? '';
let errorMessage = '';
const status = error?.response?.status;

switch (status) {
Expand Down
8 changes: 6 additions & 2 deletions playground/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ function createRequestClient(baseURL: string) {

// 通用的错误处理,如果没有进入上面的错误处理逻辑,就会进入这里
client.addResponseInterceptor(
errorMessageResponseInterceptor((msg: string, _error) => {
errorMessageResponseInterceptor((msg: string, error) => {
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
message.error(msg);
// 当前mock接口返回的错误字段是 error 或者 message
const responseData = error?.response?.data ?? {};
const errorMessage = responseData?.error ?? responseData?.message ?? '';
// 如果没有错误信息,则会根据状态码进行提示
message.error(errorMessage || msg);
}),
);

Expand Down
1 change: 0 additions & 1 deletion playground/src/views/examples/vxe-table/remote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const gridOptions: VxeGridProps<RowType> = {
{ field: 'productName', title: 'Product Name' },
{ field: 'price', title: 'Price' },
{ field: 'releaseDate', formatter: 'formatDateTime', title: 'DateTime' },
{ field: 'releaseDate', formatter: 'formatDate', title: 'Date' },
],
height: 'auto',
keepSource: true,
Expand Down

0 comments on commit ba46625

Please sign in to comment.