Files
crawler-plugin/admin-frontend-vue/src/components/load-failure-model.ts
T

72 lines
3.9 KiB
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.
/**
* 列表加载失败态模型(四面9分口径 · 后管「模板内失败态只有 4/24 页」):
* 把「请求失败了」归一成一条可读中文,配 LoadFailureBar.vue 的那个真能重跑的按钮用。
*
* 纯逻辑、不依赖 Vue 也不依赖 Element,`node --test` 直接 import 跑行为断言。
* 文案把关复用 api/user-notice:后端把底层异常原文拼进 message 时,界面只留中文主干。
*/
import { userNotice } from '../api/user-notice.ts'
/** 失败条上那个按钮的字样:必须是「重试」而不是一句「知道了」——撤入口同时得是出口。 */
export const LOAD_FAILURE_RETRY_LABEL = '重试'
/** 网络层失败(请求没到服务器就断了)给用户的中文提示。2026-11-21 线上实录:用户代理
* 链路抖动成批掐断 TLS 连接(当天全服 29 条「连接未完成即断」记录全部来自该用户的两个
* 出口 IP),axios 拿到英文 "Network Error" 按 userNotice 口径被滤空 ⇒ 界面只剩页面
* 兜底文案,用户看不出是网络问题、更不知道该重试还是该找人。这里显式补一句。 */
export const NETWORK_FAILURE_DETAIL = '网络连接中断,请检查网络后重试'
/** axios 错误且没有 response = 网络层失败(连接被掐断/超时/DNS),服务端根本没回话。
* 只认 axios 形状(code 为网络错误码,或带 config+isAxiosError 标记);普通 Error 没有
* response 不算(走原有 message 解析)。 */
function isNetworkLevelError(error: unknown): boolean {
if (!error || typeof error !== 'object') return false
const record = error as Record<string, unknown>
if (record.response !== undefined) return false
const code = typeof record.code === 'string' ? record.code : ''
if (code === 'ECONNABORTED' || code === 'ERR_NETWORK' || code === 'ETIMEDOUT' || code === 'ECONNRESET') {
return true
}
return record.isAxiosError === true && record.config !== undefined
}
/** 从 axios 错误 / Error / 字符串里取后端给的原因;取不到、或原文不是中文时返回空串。
* 例外:axios 网络层失败(无 response)不算「取不到原因」,给中文网络提示。 */
export function failureDetail(error: unknown): string {
const data = (error as { response?: { data?: unknown } } | undefined)?.response?.data
if (data && typeof data === 'object') {
const record = data as Record<string, unknown>
for (const key of ['message', 'error', 'msg']) {
const value = record[key]
if (typeof value === 'string' && value.trim()) return userNotice(value, '')
}
}
if (isNetworkLevelError(error)) return NETWORK_FAILURE_DETAIL
if (error instanceof Error && error.message.trim()) return userNotice(error.message, '')
if (typeof error === 'string' && error.trim()) return userNotice(error, '')
return ''
}
/**
* 后端按范围拒掉的句子(`无权操作该总数据`、`缺少归属公司…`)补一句人话:
* 这类失败不是页面坏了,重试一百次也是同一个结果,得把出路说清楚。
*/
export const SCOPE_REFUSAL_NOTE = '这一行不在你当前可操作的分组/公司范围内,需要由平台超级管理员处理'
const SCOPE_REFUSAL = /无权|缺少归属公司/
/** 命中范围拒绝时返回补充说明,否则空串。 */
export function scopeRefusalNote(detail: string): string {
return SCOPE_REFUSAL.test(detail || '') ? SCOPE_REFUSAL_NOTE : ''
}
/** 「本页兜底文案:后端原因」;没有可读原因时只留兜底文案,不拼出「加载失败:」这种半截句。 */
export function loadFailureMessage(error: unknown, fallback: string): string {
const head = (fallback || '').trim()
const detail = failureDetail(error)
if (!detail) return head
const note = scopeRefusalNote(detail)
const reason = note ? `${detail}(${note})` : detail
if (!head || head.includes(detail)) return head || reason
return `${head}:${reason}`
}