Files
Aether/_deprecated_py_src/services/usage/error_classifier.py
fawney19 1d9c77522a refactor: 移除 Python 后端源码,全面迁移至 Rust gateway 架构
- 删除全部 Python 源码 (src/) 及 Alembic 迁移脚本,归档至 _deprecated_py_src/
- 重构 Rust gateway ai_pipeline: 拆分 planner/finalize 模块,新增 contracts/adaptation 层
- 重组 handlers 模块为 admin/public/proxy/internal/shared 子模块结构
- 新增 executor 模块,引入 Rust 原生数据库迁移 (aether-data/migrations)
- 简化 CI/Docker 构建流程,移除 base image 二级构建,统一为单一 app image
- 移除 Python 相关基础设施文件 (entrypoint.sh, gunicorn_conf.py, Dockerfile.base)
2026-04-03 16:26:16 +08:00

71 lines
2.0 KiB
Python

"""Error classification helpers for Usage records."""
from __future__ import annotations
from src.core.enums import ErrorCategory
_STATUS_CODE_MAP: dict[int, ErrorCategory] = {
400: ErrorCategory.INVALID_REQUEST,
401: ErrorCategory.AUTH,
403: ErrorCategory.AUTH,
404: ErrorCategory.NOT_FOUND,
408: ErrorCategory.TIMEOUT,
429: ErrorCategory.RATE_LIMIT,
500: ErrorCategory.SERVER_ERROR,
502: ErrorCategory.SERVER_ERROR,
503: ErrorCategory.SERVER_ERROR,
504: ErrorCategory.TIMEOUT,
}
_CONTEXT_LENGTH_PATTERNS = (
"context_length_exceeded",
"maximum context length",
"too many tokens",
"input is too long",
)
_CONTENT_FILTER_PATTERNS = (
"content_filter",
"content_policy",
"safety_block",
"blocked by content",
)
_NETWORK_PATTERNS = ("connection", "network", "dns", "socket")
def classify_error(
status_code: int | None,
error_message: str | None,
status: str | None = None,
) -> ErrorCategory:
"""Map provider errors to ErrorCategory."""
if status and status.lower() == "cancelled":
return ErrorCategory.CANCELLED
if status_code is not None:
mapped = _STATUS_CODE_MAP.get(status_code)
if mapped:
return mapped
if error_message:
msg_lower = error_message.lower()
if any(p in msg_lower for p in _CONTEXT_LENGTH_PATTERNS):
return ErrorCategory.CONTEXT_LENGTH
if any(p in msg_lower for p in _CONTENT_FILTER_PATTERNS):
return ErrorCategory.CONTENT_FILTER
if "rate limit" in msg_lower or "rate_limit" in msg_lower:
return ErrorCategory.RATE_LIMIT
if "timeout" in msg_lower or "timed out" in msg_lower:
return ErrorCategory.TIMEOUT
if any(p in msg_lower for p in _NETWORK_PATTERNS):
return ErrorCategory.NETWORK
if status_code is not None:
if status_code >= 500:
return ErrorCategory.SERVER_ERROR
if status_code >= 400:
return ErrorCategory.INVALID_REQUEST
return ErrorCategory.UNKNOWN