Archived
refactor: HTTP 垫片异常层次收敛到 core.http_errors 单一来源
为什么:core/aiohttp_compat.py 与 core/sync_http.py 各自定义了一份 RequestException / HTTPError,是**两个不同的类**。调用方在同步段捕获 core.sync_http.HTTPError、异步段抛的却是 core.aiohttp_compat.HTTPError, 两者无继承关系——异常会穿透所有 except 分支,属于隐藏的跨层 bug。 改法:新建 core/http_errors.py 作为唯一定义处(RequestException 基类 + HTTPError / Timeout / ConnectionError),两个垫片改为从它导入并再导出。 刻意保留 requests 的命名约定(Timeout / ConnectionError 与内置同名, 调用方需带模块前缀引用),已在 docstring 写明,避免后来者误改。 注意:sync_http 补了 __all__ 显式再导出——models._RetryFromScratch 继承它、 tests/test_aria2.py 直接导入它,符号必须保持可导入。
This commit is contained in:
1 parent
2b4704183c
commit
d7d3cfce69
3 files changed
+46
-34
No files matched your search
+2
-22
@@ -21,31 +21,11 @@ from typing import Any
|
||||
|
||||
import aiohttp
|
||||
|
||||
from core.http_errors import ConnectionError, HTTPError, RequestException, Timeout
|
||||
|
||||
DEFAULT_TIMEOUT = 30
|
||||
DEFAULT_MAX_BYTES = 8 * 1024 * 1024
|
||||
|
||||
# ---------- 异常层次(对齐 requests 命名,便于原样 except) ----------
|
||||
|
||||
|
||||
class RequestException(Exception):
|
||||
"""请求异常基类(网络/协议层)。"""
|
||||
|
||||
|
||||
class HTTPError(RequestException):
|
||||
"""raise_for_status() 抛出的 HTTP 状态异常。"""
|
||||
|
||||
def __init__(self, message: str, response: 'Response | None' = None):
|
||||
super().__init__(message)
|
||||
self.response = response
|
||||
|
||||
|
||||
class Timeout(RequestException):
|
||||
"""请求超时。"""
|
||||
|
||||
|
||||
class ConnectionError(RequestException):
|
||||
"""连接错误。"""
|
||||
|
||||
|
||||
class Response:
|
||||
"""requests.Response 的异步子集:body 已读全,属性同步访问。"""
|
||||
|
||||
Reference in new issue
Block a user