diff --git a/app/utils/http/error-message.ts b/app/utils/http/error-message.ts new file mode 100644 index 0000000..cd5fa89 --- /dev/null +++ b/app/utils/http/error-message.ts @@ -0,0 +1,42 @@ +export function getApiErrorMessage(error: unknown): string { + if (error && typeof error === 'object') { + const e = error as Record + const data = e.data + if (data && typeof data === 'object' && data !== null) { + const m = (data as Record).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 '请求失败,请稍后重试' +}