MCPcopy Create free account
hub / github.com/Y80/bmm / createFetchNetworkErrorMessage

Function createFetchNetworkErrorMessage

src/utils/http.ts:79–90  ·  view source on GitHub ↗
(error: unknown)

Source from the content-addressed store, hash-verified

77
78/** 将网络错误转为用户可读的中文消息,非网络错误返回 null */
79export function createFetchNetworkErrorMessage(error: unknown): string | null {
80 if (isFetchTimeoutError(error)) return '请求超时'
81 if (!(error instanceof Error)) return null
82
83 const code = getErrorCode(error)
84 if (code === 'ENOTFOUND' || code === 'EAI_AGAIN') return '域名解析失败'
85 if (code.startsWith('CERT_') || code.includes('SSL') || code.includes('TLS')) return '证书错误'
86 if (code === 'ECONNREFUSED') return '连接被拒绝'
87 if (code === 'ETIMEDOUT') return '请求超时'
88
89 return null
90}
91
92function getErrorCode(error: unknown): string {
93 const cause = error instanceof Error && 'cause' in error ? (error as { cause?: unknown }).cause : null

Callers 2

http.test.tsFile · 0.90

Calls 2

isFetchTimeoutErrorFunction · 0.85
getErrorCodeFunction · 0.85

Tested by

no test coverage detected