task-4: extractErrorMessage 命名导出并覆盖 error/message/msg 三字段(空白跳过、优先级 error>message>msg)

This commit is contained in:
2026-08-31 19:18:57 +08:00
parent 6e227aa377
commit 9f58350b4a
2 changed files with 66 additions and 6 deletions
+8 -6
View File
@@ -25,16 +25,18 @@ export interface JavaApiResponse<T> {
}
export type RequestOptions<D = unknown> = AxiosRequestConfig<D>
function extractErrorMessage(error: unknown) {
export function extractErrorMessage(error: unknown) {
if (error instanceof AxiosError) {
const data = error.response?.data
if (data && typeof data === 'object') {
if ('error' in data && data.error) {
return String(data.error)
}
if ('message' in data && data.message) {
return String(data.message)
for (const field of ['error', 'message', 'msg'] as const) {
if (field in data && typeof (data as Record<string, unknown>)[field] === 'string') {
const value = ((data as Record<string, string>)[field]).trim()
if (value) {
return value
}
}
}
}