excode 0.1.0__cp311-cp311-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
excode/__init__.py ADDED
@@ -0,0 +1,51 @@
1
+ """
2
+ excode - 统一的异常与错误码管理包。
3
+
4
+ 提供标准化的异常类、错误码枚举以及错误处理工具函数。
5
+
6
+ Usage:
7
+ from excode import ExCodeError, ErrorCode, raise_for_error
8
+
9
+ try:
10
+ raise_for_error(ErrorCode.AUTHENTICATION_ERROR)
11
+ except ExCodeError as e:
12
+ print(e) # [1002] 认证失败,API Key 无效或过期
13
+ """
14
+
15
+ from .codes import ErrorCode, get_error_message, ERROR_CODE_MESSAGES
16
+ from .exceptions import (
17
+ ExCodeError,
18
+ ServiceError,
19
+ AuthenticationError,
20
+ RateLimitError,
21
+ QuotaExceededError,
22
+ ServiceTimeoutError,
23
+ InvalidParamsError,
24
+ PromptBanError,
25
+ ImageBanError,
26
+ InvalidImageError,
27
+ )
28
+ from .handler import raise_for_error, register_error_code
29
+
30
+ __all__ = [
31
+ # 错误码
32
+ "ErrorCode",
33
+ "get_error_message",
34
+ "ERROR_CODE_MESSAGES",
35
+ # 异常类
36
+ "ExCodeError",
37
+ "ServiceError",
38
+ "AuthenticationError",
39
+ "RateLimitError",
40
+ "QuotaExceededError",
41
+ "ServiceTimeoutError",
42
+ "InvalidParamsError",
43
+ "PromptBanError",
44
+ "ImageBanError",
45
+ "InvalidImageError",
46
+ # 工具函数
47
+ "raise_for_error",
48
+ "register_error_code",
49
+ ]
50
+
51
+ __version__ = "0.1.0"
excode/__init__.pyi ADDED
@@ -0,0 +1,5 @@
1
+ from .codes import ERROR_CODE_MESSAGES as ERROR_CODE_MESSAGES, ErrorCode as ErrorCode, get_error_message as get_error_message
2
+ from .exceptions import AuthenticationError as AuthenticationError, ExCodeError as ExCodeError, ImageBanError as ImageBanError, InvalidImageError as InvalidImageError, InvalidParamsError as InvalidParamsError, PromptBanError as PromptBanError, QuotaExceededError as QuotaExceededError, RateLimitError as RateLimitError, ServiceError as ServiceError, ServiceTimeoutError as ServiceTimeoutError
3
+ from .handler import raise_for_error as raise_for_error, register_error_code as register_error_code
4
+
5
+ __all__ = ['ErrorCode', 'get_error_message', 'ERROR_CODE_MESSAGES', 'ExCodeError', 'ServiceError', 'AuthenticationError', 'RateLimitError', 'QuotaExceededError', 'ServiceTimeoutError', 'InvalidParamsError', 'PromptBanError', 'ImageBanError', 'InvalidImageError', 'raise_for_error', 'register_error_code']
Binary file
excode/codes.pyi ADDED
@@ -0,0 +1,28 @@
1
+ from enum import IntEnum
2
+
3
+ class ErrorCode(IntEnum):
4
+ """
5
+ excode 错误码枚举。
6
+
7
+ 错误码规则:
8
+ - 1xxx: 通用/服务相关错误
9
+ - 2xxx: 参数相关错误
10
+ - 3xxx: 内容安全相关错误
11
+ - 4xxx: 图片相关错误
12
+ """
13
+ SUCCESS = 0
14
+ UNKNOWN_ERROR = 1000
15
+ SERVICE_ERROR = 1001
16
+ AUTHENTICATION_ERROR = 1002
17
+ RATE_LIMIT = 1003
18
+ QUOTA_EXCEEDED = 1004
19
+ SERVICE_TIMEOUT = 1005
20
+ INVALID_PARAMS = 2001
21
+ PROMPT_BANNED = 3001
22
+ IMAGE_BANNED = 3002
23
+ INVALID_IMAGE = 4001
24
+
25
+ ERROR_CODE_MESSAGES: dict[ErrorCode, str]
26
+
27
+ def get_error_message(code: ErrorCode) -> str:
28
+ """根据错误码获取对应的错误描述"""
Binary file
excode/exceptions.pyi ADDED
@@ -0,0 +1,77 @@
1
+ from .codes import ErrorCode as ErrorCode
2
+ from _typeshed import Incomplete
3
+ from typing import Any
4
+
5
+ class ExCodeError(Exception):
6
+ """
7
+ excode 包的基础异常类。
8
+
9
+ 所有 excode 异常都继承自此类,可通过 `except ExCodeError` 统一捕获。
10
+
11
+ Attributes:
12
+ error_code: 错误码枚举值
13
+ error_msg: 错误描述
14
+ extra_data: 额外上下文数据
15
+ """
16
+ error_code: ErrorCode
17
+ error_msg: Incomplete
18
+ extra_data: Incomplete
19
+ def __init__(self, error_msg: str, extra_data: dict[str, Any] | None = None, **kwargs: Any) -> None: ...
20
+
21
+ class ServiceError(ExCodeError):
22
+ """通用服务错误"""
23
+ error_code: Incomplete
24
+
25
+ class AuthenticationError(ExCodeError):
26
+ """认证失败,API Key 无效或过期"""
27
+ error_code: Incomplete
28
+
29
+ class RateLimitError(ExCodeError):
30
+ """请求频率超限"""
31
+ error_code: Incomplete
32
+
33
+ class QuotaExceededError(ExCodeError):
34
+ """配额/余额不足"""
35
+ error_code: Incomplete
36
+
37
+ class ServiceTimeoutError(ExCodeError):
38
+ """服务超时"""
39
+ error_code: Incomplete
40
+
41
+ class InvalidParamsError(ExCodeError):
42
+ """请求参数无效"""
43
+ error_code: Incomplete
44
+
45
+ class PromptBanError(ExCodeError):
46
+ """
47
+ 提示词违禁异常。
48
+
49
+ Attributes:
50
+ error_msg: 错误描述
51
+ extend_data: 命中的违禁词列表
52
+ extra_data: 额外上下文数据
53
+ """
54
+ error_code: Incomplete
55
+ extend_data: Incomplete
56
+ def __init__(self, error_msg: str, data: Any = None, extra_data: dict[str, Any] | None = None, **kwargs: Any) -> None: ...
57
+
58
+ class ImageBanError(ExCodeError):
59
+ """图片包含违禁内容"""
60
+ error_code: Incomplete
61
+
62
+ class InvalidImageError(ExCodeError):
63
+ """
64
+ 图片无效异常。
65
+
66
+ 当图片损坏、格式错误或无法解码时抛出。
67
+
68
+ Attributes:
69
+ error_msg: 错误描述
70
+ image_size: 图片数据大小(字节数)
71
+ original_error: 原始异常
72
+ extra_data: 额外上下文数据
73
+ """
74
+ error_code: Incomplete
75
+ image_size: Incomplete
76
+ original_error: Incomplete
77
+ def __init__(self, error_msg: str, image_size: int | None = None, original_error: Exception | None = None, extra_data: dict[str, Any] | None = None, **kwargs: Any) -> None: ...
Binary file
excode/handler.pyi ADDED
@@ -0,0 +1,25 @@
1
+ from .codes import ErrorCode as ErrorCode, get_error_message as get_error_message
2
+ from .exceptions import AuthenticationError as AuthenticationError, ExCodeError as ExCodeError, ImageBanError as ImageBanError, InvalidImageError as InvalidImageError, InvalidParamsError as InvalidParamsError, PromptBanError as PromptBanError, QuotaExceededError as QuotaExceededError, RateLimitError as RateLimitError, ServiceError as ServiceError, ServiceTimeoutError as ServiceTimeoutError
3
+ from typing import Any
4
+
5
+ def raise_for_error(error_code: ErrorCode, error_msg: str | None = None, extra_data: dict[str, Any] | None = None, **kwargs: Any) -> None:
6
+ """
7
+ 根据错误码抛出对应的异常。
8
+
9
+ Args:
10
+ error_code: 错误码
11
+ error_msg: 错误描述,为 None 时使用默认描述
12
+ extra_data: 额外上下文数据
13
+ **kwargs: 其他关键字参数
14
+
15
+ Raises:
16
+ ExCodeError: 对应错误码的异常
17
+ """
18
+ def register_error_code(error_code: ErrorCode, exc_class: type[ExCodeError]) -> None:
19
+ """
20
+ 注册自定义的错误码到异常类的映射。
21
+
22
+ Args:
23
+ error_code: 错误码
24
+ exc_class: 对应的异常类
25
+ """
@@ -0,0 +1,186 @@
1
+ Metadata-Version: 2.1
2
+ Name: excode
3
+ Version: 0.1.0
4
+ Summary: 统一的异常与错误码管理包
5
+ Home-page: https://github.com/zhenzi0322/excode
6
+ Author:
7
+ Author-email: <>
8
+ License: MIT
9
+ Requires-Python: >=3.8
10
+ description-content-type: text/markdown
11
+ Description:
12
+ # excode
13
+
14
+ 统一的异常与错误码管理 Python 包,提供标准化的异常类、错误码枚举以及错误处理工具函数。
15
+
16
+ ## 安装
17
+
18
+ ```bash
19
+ pip install excode
20
+ ```
21
+
22
+ ## 快速开始
23
+
24
+ ```python
25
+ from excode import ExCodeError, ErrorCode, raise_for_error
26
+
27
+ # 根据错误码自动抛出对应异常
28
+ try:
29
+ raise_for_error(ErrorCode.AUTHENTICATION_ERROR)
30
+ except ExCodeError as e:
31
+ print(e) # [1002] 认证失败,API Key 无效或过期
32
+ ```
33
+
34
+ ## 错误码一览
35
+
36
+ | 错误码 | 枚举值 | 说明 |
37
+ |--------|--------|------|
38
+ | `0` | `ErrorCode.SUCCESS` | 成功 |
39
+ | `1000` | `ErrorCode.UNKNOWN_ERROR` | 未知错误 |
40
+ | `1001` | `ErrorCode.SERVICE_ERROR` | 服务错误 |
41
+ | `1002` | `ErrorCode.AUTHENTICATION_ERROR` | 认证失败 |
42
+ | `1003` | `ErrorCode.RATE_LIMIT` | 请求频率超限 |
43
+ | `1004` | `ErrorCode.QUOTA_EXCEEDED` | 配额/余额不足 |
44
+ | `1005` | `ErrorCode.SERVICE_TIMEOUT` | 服务超时 |
45
+ | `2001` | `ErrorCode.INVALID_PARAMS` | 请求参数无效 |
46
+ | `3001` | `ErrorCode.PROMPT_BANNED` | 提示词违禁 |
47
+ | `3002` | `ErrorCode.IMAGE_BANNED` | 图片违禁 |
48
+ | `4001` | `ErrorCode.INVALID_IMAGE` | 图片无效或损坏 |
49
+
50
+ ## 异常类层级
51
+
52
+ 所有异常均继承自 `ExCodeError`,可通过 `except ExCodeError` 统一捕获。
53
+
54
+ ```
55
+ ExCodeError
56
+ ├── ServiceError # 通用服务错误
57
+ ├── AuthenticationError # 认证失败
58
+ ├── RateLimitError # 请求频率超限
59
+ ├── QuotaExceededError # 配额/余额不足
60
+ ├── ServiceTimeoutError # 服务超时
61
+ ├── InvalidParamsError # 请求参数无效
62
+ ├── PromptBanError # 提示词违禁
63
+ ├── ImageBanError # 图片违禁
64
+ └── InvalidImageError # 图片无效或损坏
65
+ ```
66
+
67
+ ## 使用方式
68
+
69
+ ### 1. 直接抛出异常
70
+
71
+ ```python
72
+ from excode import AuthenticationError
73
+
74
+ raise AuthenticationError("API Key 已过期", extra_data={"key_id": "abc123"})
75
+ # 输出: [1002] API Key 已过期 [{'key_id': 'abc123'}]
76
+ ```
77
+
78
+ ### 2. 根据错误码抛出(工厂函数)
79
+
80
+ ```python
81
+ from excode import ErrorCode, raise_for_error
82
+
83
+ # 使用默认描述
84
+ raise_for_error(ErrorCode.RATE_LIMIT)
85
+ # 输出: [1003] 请求频率超限,请稍后重试
86
+
87
+ # 使用自定义描述
88
+ raise_for_error(ErrorCode.RATE_LIMIT, error_msg="当前接口调用次数已达上限")
89
+ # 输出: [1003] 当前接口调用次数已达上限
90
+ ```
91
+
92
+ ### 3. 统一捕获并处理
93
+
94
+ ```python
95
+ from excode import ExCodeError, ErrorCode, raise_for_error
96
+
97
+ try:
98
+ raise_for_error(ErrorCode.QUOTA_EXCEEDED)
99
+ except ExCodeError as e:
100
+ print(e.error_code) # ErrorCode.QUOTA_EXCEEDED
101
+ print(e.error_code.value) # 1004
102
+ print(e.error_msg) # 配额/余额不足
103
+ print(e.extra_data) # {}
104
+ ```
105
+
106
+ ### 4. 分类捕获
107
+
108
+ ```python
109
+ from excode import AuthenticationError, RateLimitError, ExCodeError
110
+
111
+ try:
112
+ # 业务逻辑
113
+ ...
114
+ except AuthenticationError as e:
115
+ # 处理认证失败
116
+ refresh_api_key()
117
+ except RateLimitError as e:
118
+ # 处理限流
119
+ wait_and_retry()
120
+ except ExCodeError as e:
121
+ # 兜底处理其他 excode 异常
122
+ log_error(e)
123
+ ```
124
+
125
+ ### 5. 携带额外上下文
126
+
127
+ ```python
128
+ from excode import InvalidParamsError
129
+
130
+ raise InvalidParamsError(
131
+ "参数校验失败",
132
+ extra_data={"field": "width", "value": -1, "reason": "必须为正整数"}
133
+ )
134
+ # 输出: [2001] 参数校验失败 [{'field': 'width', 'value': -1, 'reason': '必须为正整数'}]
135
+ ```
136
+
137
+ ### 6. 特殊异常的扩展属性
138
+
139
+ ```python
140
+ from excode import PromptBanError, InvalidImageError
141
+
142
+ # PromptBanError 携带命中的违禁词列表
143
+ raise PromptBanError("提示词违禁", data=["暴力", "色情"])
144
+
145
+ # InvalidImageError 携带图片尺寸和原始异常
146
+ raise InvalidImageError("图片无法解码", image_size=2048, original_error=decode_err)
147
+ ```
148
+
149
+ ### 7. 获取错误码描述
150
+
151
+ ```python
152
+ from excode import ErrorCode, get_error_message
153
+
154
+ msg = get_error_message(ErrorCode.SERVICE_TIMEOUT)
155
+ print(msg) # 服务超时,请稍后重试
156
+ ```
157
+
158
+ ### 8. 注册自定义错误码映射
159
+
160
+ ```python
161
+ from enum import IntEnum
162
+ from excode import ExCodeError, ErrorCode, raise_for_error, register_error_code
163
+
164
+ # 定义自定义异常
165
+ class NetworkError(ExCodeError):
166
+ error_code = ErrorCode.UNKNOWN_ERROR # 可复用或自行扩展
167
+
168
+ # 注册映射
169
+ register_error_code(ErrorCode.SERVICE_ERROR, NetworkError)
170
+
171
+ # 之后 raise_for_error 会使用你注册的异常类
172
+ raise_for_error(ErrorCode.SERVICE_ERROR, error_msg="网络连接失败")
173
+ ```
174
+
175
+ ## 技术架构
176
+
177
+ ```
178
+ excode/
179
+ ├── __init__.py # 包入口,统一导出所有公开 API
180
+ ├── codes.py # ErrorCode 枚举、错误码描述映射
181
+ ├── exceptions.py # 异常类定义(继承体系)
182
+ └── handler.py # raise_for_error 工厂函数、register_error_code 注册函数
183
+ ```
184
+
185
+
186
+
@@ -0,0 +1,12 @@
1
+ excode/codes.cp311-win_amd64.pyd,sha256=De1OwnAnhx1yy4_wVN892Mek-N7XDlr8_FI9v8bFEl8,43520
2
+ excode/codes.pyi,sha256=51WD0qCG3oxdNnEo6PM0OjxBM2-esk2DY5_WdX562ic,720
3
+ excode/exceptions.cp311-win_amd64.pyd,sha256=rMJkHKgGcnXhEwhI58swxXy6pbfiL18xUsHoqM5yhuQ,68608
4
+ excode/exceptions.pyi,sha256=iolGjsSLZsz0ZHnQALVQF1uW2vpf3zc5AgDiURdeph0,2341
5
+ excode/handler.cp311-win_amd64.pyd,sha256=b6XDMgx6Oo0jWIFhOO-BF2tuN9ZJDQQ3_bG-XFUOIv0,51200
6
+ excode/handler.pyi,sha256=4ZPUHut89cBWW-IDM_liY0NAewNWv1-tsnK7kyeG7vo,1207
7
+ excode/__init__.py,sha256=vsrn-Bn28ACuGSyWnbV1gB13135s6uyxdLwSslnQOqg,1234
8
+ excode/__init__.pyi,sha256=VA4i2N4qpUduIQcJSRtqfyT-6sHKGXxyqtwuWAxU_m4,930
9
+ excode/__pycache__/__init__.cpython-311.pyc,sha256=euqUuzdeUuMT-oJBmG2_WaFr1Zynj5k8T2AQAfrMsFs,1315
10
+ excode-0.1.0.dist-info/METADATA,sha256=S5No4QFWXdVsDvXpkgDoSOGTvqZkpNXq6549oLL0fkc,5442
11
+ excode-0.1.0.dist-info/WHEEL,sha256=GwgCn-AMHvmo53PcAXsuRM3P8FHUe3mSLgJWY38-9rM,109
12
+ excode-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: excode-protected-builder
3
+ Root-Is-Purelib: false
4
+ Tag: cp311-cp311-win_amd64