97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
import type { ApiResponse } from '@/types/api'
|
|
|
|
export class ApiError extends Error {
|
|
code?: number
|
|
|
|
constructor(message: string, code?: number) {
|
|
super(message)
|
|
this.name = 'ApiError'
|
|
this.code = code
|
|
}
|
|
}
|
|
|
|
export function isApiSuccess(body: ApiResponse): boolean {
|
|
if (body.code === undefined) return true
|
|
return body.code === 200 || body.code === 0
|
|
}
|
|
|
|
export function unwrapApiData<T>(body: ApiResponse<T> & Record<string, unknown>): T {
|
|
if (body.data !== undefined && body.data !== null) {
|
|
return body.data as T
|
|
}
|
|
return body as T
|
|
}
|
|
|
|
const NETWORK_ERROR_PATTERN = /request:fail|uploadFile:fail|downloadFile:fail|chooseImage:fail|connectSocket:fail|network\s*error|network\s*request\s*failed|failed\s+to\s+fetch|fetch\s+failed|load\s+failed|unable to resolve host|no address associated|failed to connect|connection\s*(refused|reset|timed out|timeout)|timed?\s*out|offline|statuscode:\s*(null|-1)|err_connection|net::|the internet connection appears to be/i
|
|
|
|
const UNI_API_FAIL_PATTERN = /(request|uploadFile|downloadFile|chooseImage|connectSocket):fail\b/i
|
|
|
|
function hasChinese(text: string): boolean {
|
|
return /[\u4e00-\u9fff]/.test(text)
|
|
}
|
|
|
|
function isEnglishSystemError(text: string): boolean {
|
|
const value = text.trim()
|
|
if (!value) return true
|
|
if (hasChinese(value)) return false
|
|
return /[a-z]/i.test(value)
|
|
}
|
|
|
|
function collectErrorTexts(err: unknown): string[] {
|
|
const texts: string[] = []
|
|
if (typeof err === 'string') {
|
|
texts.push(err)
|
|
return texts
|
|
}
|
|
if (!err || typeof err !== 'object') return texts
|
|
|
|
const record = err as Record<string, unknown>
|
|
if (typeof record.message === 'string') texts.push(record.message)
|
|
if (typeof record.errMsg === 'string') texts.push(record.errMsg)
|
|
if (typeof record.msg === 'string') texts.push(record.msg)
|
|
|
|
if (err instanceof Error && err.message) {
|
|
texts.push(err.message)
|
|
}
|
|
|
|
return [...new Set(texts.filter(Boolean))]
|
|
}
|
|
|
|
/** 将 uni.request / uploadFile / fetch 等系统层英文网络错误转为中文提示 */
|
|
export function normalizeNetworkErrorMessage(
|
|
message?: string,
|
|
fallback = '网络异常,请检查网络后重试',
|
|
): string {
|
|
const text = message?.trim() ?? ''
|
|
if (!text) return fallback
|
|
if (NETWORK_ERROR_PATTERN.test(text)) return fallback
|
|
if (UNI_API_FAIL_PATTERN.test(text)) return fallback
|
|
if (isEnglishSystemError(text) && /fail|error|timeout|network|statuscode|upload|request|socket|fetch|offline|null/i.test(text)) {
|
|
return fallback
|
|
}
|
|
return text
|
|
}
|
|
|
|
export function getErrorMessage(err: unknown, fallback = '请求失败'): string {
|
|
const texts = collectErrorTexts(err)
|
|
|
|
for (const text of texts) {
|
|
const normalized = normalizeNetworkErrorMessage(text, fallback)
|
|
if (normalized !== text) return normalized
|
|
if (!isEnglishSystemError(text)) return text
|
|
}
|
|
|
|
if (err && typeof err === 'object' && 'data' in err) {
|
|
const data = (err as { data?: ApiResponse }).data
|
|
if (data?.msg && !isEnglishSystemError(data.msg)) return data.msg
|
|
if (data?.message && !isEnglishSystemError(data.message)) return data.message
|
|
}
|
|
|
|
return fallback
|
|
}
|
|
|
|
/** 文件上传失败提示(无网 / 系统层错误统一中文) */
|
|
export function getUploadErrorMessage(err: unknown): string {
|
|
return getErrorMessage(err, '网络异常,请检查网络后重试')
|
|
}
|