fix:最新
This commit is contained in:
@@ -22,9 +22,30 @@ export function unwrapApiData<T>(body: ApiResponse<T> & Record<string, unknown>)
|
||||
return body as T
|
||||
}
|
||||
|
||||
const NETWORK_ERROR_PATTERN = /request:fail|network\s*error|unable to resolve host|no address associated|failed to connect|connection\s*(refused|reset|timed out|timeout)|timed?\s*out|offline|statuscode:\s*-1|err_connection|net::/i
|
||||
|
||||
/** 将 uni.request / 系统层英文网络错误转为中文提示 */
|
||||
export function normalizeNetworkErrorMessage(
|
||||
message?: string,
|
||||
fallback = '网络异常,请检查网络后重试',
|
||||
): string {
|
||||
const text = message?.trim() ?? ''
|
||||
if (!text) return fallback
|
||||
if (NETWORK_ERROR_PATTERN.test(text)) return fallback
|
||||
return text
|
||||
}
|
||||
|
||||
export function getErrorMessage(err: unknown, fallback = '请求失败'): string {
|
||||
if (err instanceof ApiError) return err.message
|
||||
if (err instanceof Error && err.message) return err.message
|
||||
if (err instanceof ApiError) {
|
||||
return normalizeNetworkErrorMessage(err.message, fallback)
|
||||
}
|
||||
if (err instanceof Error && err.message) {
|
||||
return normalizeNetworkErrorMessage(err.message, fallback)
|
||||
}
|
||||
if (err && typeof err === 'object' && 'errMsg' in err) {
|
||||
const errMsg = String((err as { errMsg?: string }).errMsg || '')
|
||||
if (errMsg) return normalizeNetworkErrorMessage(errMsg, fallback)
|
||||
}
|
||||
if (err && typeof err === 'object' && 'data' in err) {
|
||||
const data = (err as { data?: ApiResponse }).data
|
||||
if (data?.msg) return data.msg
|
||||
|
||||
Reference in New Issue
Block a user