fix(admin,api): 上分超额提示而非静默截断,并返回中文业务错误
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -15,7 +15,18 @@ export function resolveFormError(e: unknown, t: (key: string) => string): string
|
||||
return t('msg.form_invalid');
|
||||
}
|
||||
|
||||
/** 从 API 错误响应提取可读文案(Nest 全局过滤器返回 `error` 字段) */
|
||||
/** Nest 默认 4xx 响应里 error 常为 "Bad Request" 等泛化文案,真实原因在 message */
|
||||
const GENERIC_HTTP_ERRORS = new Set([
|
||||
'Bad Request',
|
||||
'Unauthorized',
|
||||
'Forbidden',
|
||||
'Not Found',
|
||||
'Conflict',
|
||||
'Unprocessable Entity',
|
||||
'Internal Server Error',
|
||||
]);
|
||||
|
||||
/** 从 API 错误响应提取可读文案(兼容 GlobalExceptionFilter 与 Nest 默认格式) */
|
||||
export function resolveApiError(
|
||||
err: unknown,
|
||||
t: (key: string) => string,
|
||||
@@ -23,8 +34,13 @@ export function resolveApiError(
|
||||
): string {
|
||||
const data = (err as { response?: { data?: { error?: string | string[]; message?: string | string[] } } })
|
||||
?.response?.data;
|
||||
const raw = data?.error ?? data?.message;
|
||||
if (Array.isArray(raw)) return raw.join(';');
|
||||
if (typeof raw === 'string' && raw.trim()) return raw;
|
||||
const msgRaw = data?.message;
|
||||
const errRaw = data?.error;
|
||||
const message = Array.isArray(msgRaw) ? msgRaw.join(';') : msgRaw;
|
||||
const error = Array.isArray(errRaw) ? errRaw.join(';') : errRaw;
|
||||
if (typeof message === 'string' && message.trim()) {
|
||||
if (!error || GENERIC_HTTP_ERRORS.has(error)) return message;
|
||||
}
|
||||
if (typeof error === 'string' && error.trim() && !GENERIC_HTTP_ERRORS.has(error)) return error;
|
||||
return t(fallbackKey);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user