1 changed files with 42 additions and 0 deletions
@ -0,0 +1,42 @@ |
|||||
|
export function getApiErrorMessage(error: unknown): string { |
||||
|
if (error && typeof error === 'object') { |
||||
|
const e = error as Record<string, unknown> |
||||
|
const data = e.data |
||||
|
if (data && typeof data === 'object' && data !== null) { |
||||
|
const m = (data as Record<string, unknown>).message |
||||
|
if (typeof m === 'string' && m.trim().length > 0) { |
||||
|
return m |
||||
|
} |
||||
|
} |
||||
|
const sm = e.statusMessage |
||||
|
if (typeof sm === 'string' && sm.trim().length > 0) { |
||||
|
return sm |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (error instanceof Error && error.message.trim().length > 0) { |
||||
|
return error.message |
||||
|
} |
||||
|
|
||||
|
if (error && typeof error === 'object' && 'statusCode' in error) { |
||||
|
const code = (error as { statusCode?: number }).statusCode |
||||
|
if (code === 401) { |
||||
|
return '登录已失效,请重新登录' |
||||
|
} |
||||
|
if (code === 403) { |
||||
|
return '没有权限执行此操作' |
||||
|
} |
||||
|
if (code === 404) { |
||||
|
return '请求的资源不存在' |
||||
|
} |
||||
|
if (typeof code === 'number' && code >= 500) { |
||||
|
return '服务暂时不可用,请稍后重试' |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (error instanceof TypeError) { |
||||
|
return '网络异常,请检查连接后重试' |
||||
|
} |
||||
|
|
||||
|
return '请求失败,请稍后重试' |
||||
|
} |
||||
Loading…
Reference in new issue