Files
asset-tracker/web/frontend/src/utils/errors.ts

19 lines
864 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function getErrorMessage(err: any, fallback = '请求失败,请稍后重试') {
const status = err?.response?.status
const serverMsg = err?.response?.data?.message
if (serverMsg && typeof serverMsg === 'string') {
const rid = err?.response?.data?.request_id
return rid ? `${serverMsg}(请求编号: ${rid}` : serverMsg
}
let msg = fallback
if (status === 400) msg = '请求参数有误,请检查后重试'
else if (status === 401) msg = '登录状态已失效,请重新登录'
else if (status === 403) msg = '你没有权限执行该操作'
else if (status === 404) msg = '请求资源不存在'
else if (status >= 500) msg = '服务暂时不可用,请稍后重试'
const rid = err?.response?.data?.request_id || err?.response?.headers?.['x-request-id']
return rid ? `${msg}(请求编号: ${rid}` : msg
}