浏览代码

Show the API error text instead of wrapping it with a traceId.

Inventory SIGNAL failures can display the Chinese reason returned by the backend.

Co-authored-by: Cursor <[email protected]>
fix負數倉
kelvin.yau 1 天前
父节点
当前提交
a9db46af61
共有 1 个文件被更改,包括 26 次插入2 次删除
  1. +26
    -2
      src/app/utils/fetchUtil.ts

+ 26
- 2
src/app/utils/fetchUtil.ts 查看文件

@@ -24,6 +24,27 @@ export interface searchParamsProps {
searchParams: { [key: string]: string | string[] | undefined };
}

/** Prefer API `error` / `message` so SIGNAL 400s show the Chinese reason, not a traceId. */
function userMessageFromErrorBody(body: string, fallback: string): string {
const trimmed = body.trim();
if (!trimmed) return fallback;
try {
const errorJson = JSON.parse(trimmed) as {
error?: unknown;
message?: unknown;
};
if (typeof errorJson.error === "string" && errorJson.error.trim()) {
return errorJson.error.trim();
}
if (typeof errorJson.message === "string" && errorJson.message.trim()) {
return errorJson.message.trim();
}
} catch {
return trimmed;
}
return trimmed;
}

export async function serverFetchWithNoContent(...args: FetchParams) {
const response = await serverFetch(...args);

@@ -57,7 +78,7 @@ export async function serverFetchWithNoContent(...args: FetchParams) {
}
console.error(`Server error (${response.status}):`, errorMessage);
throw new ServerFetchError(
`Server error: ${response.status} ${response.statusText}. ${errorMessage}`,
userMessageFromErrorBody(errorMessage, errorMessage),
response
);
}
@@ -107,7 +128,10 @@ export async function serverFetchJson<T>(...args: FetchParams) {
signOutUser();
default:
throw new ServerFetchError(
`Server error: ${response.status} ${response.statusText}. ${errorText}`,
userMessageFromErrorBody(
errorText,
`Server error: ${response.status} ${response.statusText}`,
),
response,
);
}


正在加载...
取消
保存