| 70 | |
| 71 | |
| 72 | def classify_error(error: Union[Exception, str], context: Optional[Dict[str, Any]] = None) -> AppError: |
| 73 | raw = str(error) |
| 74 | text = raw.lower() |
| 75 | diagnostics = _sanitize_diagnostics(context or {}) |
| 76 | diagnostics["raw"] = _summarize_raw(raw, 1000) |
| 77 | |
| 78 | status, upstream_message = _extract_status_and_message(raw) |
| 79 | if status: |
| 80 | diagnostics["http_status"] = status |
| 81 | if upstream_message: |
| 82 | diagnostics["upstream_message"] = _truncate(upstream_message, 500) |
| 83 | |
| 84 | host = _extract_host(raw) or diagnostics.get("base_url") or diagnostics.get("host") |
| 85 | endpoint = diagnostics.get("endpoint") or _extract_endpoint(raw) |
| 86 | |
| 87 | if _looks_like_proxy_refused(text): |
| 88 | return AppError( |
| 89 | code="PROXY_UNAVAILABLE", |
| 90 | title="本机代理不可用", |
| 91 | detail="请求需要经过本机代理,但代理端口没有响应。", |
| 92 | suggestion="请确认代理客户端已开启,且 HTTP/HTTPS 代理端口正在监听;不使用代理时请关闭相关环境变量。", |
| 93 | status=400, |
| 94 | retryable=True, |
| 95 | diagnostics=diagnostics, |
| 96 | ) |
| 97 | |
| 98 | if _looks_like_fake_ip_tls(text): |
| 99 | detail = f"无法稳定连接到 {host}" if host else "上游 TLS 连接在握手阶段被断开。" |
| 100 | return AppError( |
| 101 | code="NETWORK_FAKE_IP_TLS", |
| 102 | title="网络代理连接异常", |
| 103 | detail=detail, |
| 104 | suggestion="请确认代理客户端已开启,或关闭 Fake-IP DNS 后重试。", |
| 105 | status=400, |
| 106 | retryable=True, |
| 107 | diagnostics=diagnostics, |
| 108 | ) |
| 109 | |
| 110 | if "不支持 /v1/chat/completions" in raw or ( |
| 111 | "not support" in text and "chat/completions" in text |
| 112 | ) or ( |
| 113 | "unsupported" in text and "chat/completions" in text |
| 114 | ): |
| 115 | return AppError( |
| 116 | code="MODEL_ENDPOINT_MISMATCH", |
| 117 | title="模型与接口不匹配", |
| 118 | detail=upstream_message or "当前模型不支持 OpenAI Chat Completions 接口。", |
| 119 | suggestion="请在设置中换成支持该接口的文字模型,或调整该服务商的 endpoint_type。", |
| 120 | status=400, |
| 121 | retryable=False, |
| 122 | diagnostics=diagnostics, |
| 123 | ) |
| 124 | |
| 125 | if "unknown parameter" in text and "response_format" in text: |
| 126 | return AppError( |
| 127 | code="UPSTREAM_PARAM_UNSUPPORTED", |
| 128 | title="上游参数不兼容", |
| 129 | detail="服务商不支持 response_format 参数。", |