You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

42 lines
1.2 KiB

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 '请求失败,请稍后重试'
}