diff --git a/src/app/utils/fetchUtil.ts b/src/app/utils/fetchUtil.ts index aa445854..22327ebc 100644 --- a/src/app/utils/fetchUtil.ts +++ b/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(...args: FetchParams) { signOutUser(); default: throw new ServerFetchError( - `Server error: ${response.status} ${response.statusText}. ${errorText}`, + userMessageFromErrorBody( + errorText, + `Server error: ${response.status} ${response.statusText}`, + ), response, ); }