|
@@ -35,15 +35,22 @@ export interface ServiceResponse<T = any> {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/** 自定义错误类 */
|
|
/** 自定义错误类 */
|
|
|
|
|
+export interface HttpErrorOptions {
|
|
|
|
|
+ code?: string | number
|
|
|
|
|
+ status?: number
|
|
|
|
|
+ data?: any
|
|
|
|
|
+ response?: any
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export class HttpError extends Error {
|
|
export class HttpError extends Error {
|
|
|
code?: string | number
|
|
code?: string | number
|
|
|
status?: number
|
|
status?: number
|
|
|
data?: any
|
|
data?: any
|
|
|
- constructor(message: string, options?: { code?: string | number; status?: number; data?: any }) {
|
|
|
|
|
|
|
+ response?: any
|
|
|
|
|
+
|
|
|
|
|
+ constructor(message: string, options: HttpErrorOptions = {}) {
|
|
|
super(message)
|
|
super(message)
|
|
|
- this.code = options?.code
|
|
|
|
|
- this.status = options?.status
|
|
|
|
|
- this.data = options?.data
|
|
|
|
|
|
|
+ Object.assign(this, options)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -81,8 +88,31 @@ const abortAllRequests = () => {
|
|
|
|
|
|
|
|
const handleNetworkError = (response: AxiosResponse, silent?: boolean) => {
|
|
const handleNetworkError = (response: AxiosResponse, silent?: boolean) => {
|
|
|
const status = response?.status
|
|
const status = response?.status
|
|
|
|
|
+ const backendCode = response?.data?.code
|
|
|
|
|
+ const backendMsg = response?.data?.msg
|
|
|
|
|
+ const backendData = response?.data?.data
|
|
|
|
|
+
|
|
|
|
|
+ // 默认错误信息
|
|
|
let message = '未知错误'
|
|
let message = '未知错误'
|
|
|
|
|
|
|
|
|
|
+ // ✅ 优先使用后端业务错误(即 code !== 0)
|
|
|
|
|
+ if (backendCode && backendCode !== 0) {
|
|
|
|
|
+ message = backendMsg || '业务处理失败'
|
|
|
|
|
+
|
|
|
|
|
+ if (!silent) {
|
|
|
|
|
+ // showToast(message)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ✅ 抛出包含后端信息的 HttpError
|
|
|
|
|
+ throw new HttpError(message, {
|
|
|
|
|
+ code: backendCode,
|
|
|
|
|
+ status,
|
|
|
|
|
+ data: backendData,
|
|
|
|
|
+ response: response.data,
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 🚨 否则走 HTTP 状态码分支
|
|
|
switch (status) {
|
|
switch (status) {
|
|
|
case 400:
|
|
case 400:
|
|
|
message = '错误的请求'
|
|
message = '错误的请求'
|
|
@@ -119,12 +149,17 @@ const handleNetworkError = (response: AxiosResponse, silent?: boolean) => {
|
|
|
message = `其他错误:${status}`
|
|
message = `其他错误:${status}`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const msg = response.data?.msg || response.data?.data || message
|
|
|
|
|
|
|
+ const msg = backendMsg || backendData || message
|
|
|
if (!silent) {
|
|
if (!silent) {
|
|
|
showToast(msg)
|
|
showToast(msg)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- throw new HttpError(msg, { code: response.data?.code, status, data: response.data })
|
|
|
|
|
|
|
+ throw new HttpError(msg, {
|
|
|
|
|
+ code: backendCode || 'HTTP_ERROR',
|
|
|
|
|
+ status,
|
|
|
|
|
+ data: backendData,
|
|
|
|
|
+ response: response.data,
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* -------------------------------------------------------------------------- */
|