(error: unknown)
| 77 | |
| 78 | /** 将网络错误转为用户可读的中文消息,非网络错误返回 null */ |
| 79 | export 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 | |
| 92 | function getErrorCode(error: unknown): string { |
| 93 | const cause = error instanceof Error && 'cause' in error ? (error as { cause?: unknown }).cause : null |
no test coverage detected