Skip to main content

统一的异常与错误码管理包

Project description

excode

PyPI version Python License Documentation Status

统一的异常与错误码管理Python包,提供标准化的异常类、错误码枚举以及错误处理工具函数。

安装

pip install excode

快速开始

from excode import ExCodeError, ErrorCode, raise_for_error

# 根据错误码自动抛出对应异常
try:
    raise_for_error(ErrorCode.AUTHENTICATION_ERROR)
except ExCodeError as e:
    print(e)  # [1002] 认证失败,API Key 无效或过期

错误码一览

错误码 枚举值 说明
0 ErrorCode.SUCCESS 成功
1000 ErrorCode.UNKNOWN_ERROR 未知错误
1001 ErrorCode.SERVICE_ERROR 服务错误
1002 ErrorCode.AUTHENTICATION_ERROR 认证失败,API Key 无效或过期
1003 ErrorCode.RATE_LIMIT 请求频率超限,请稍后重试
1004 ErrorCode.QUOTA_EXCEEDED 配额/余额不足
1005 ErrorCode.SERVICE_TIMEOUT 服务超时,请稍后重试
1006 ErrorCode.SERVICE_BUSY 服务繁忙,请稍后再试!
2001 ErrorCode.INVALID_PARAMS 请求参数无效
3001 ErrorCode.PROMPT_BANNED 提示词包含违禁内容
3002 ErrorCode.IMAGE_BANNED 图片包含违禁内容
3003 ErrorCode.CONTENT_POLICY_VIOLATION 您的请求包含违反平台政策的内容,请调整图像或提示词后重试
4001 ErrorCode.INVALID_IMAGE 图片无效或损坏
4002 ErrorCode.IMAGE_GENERATE_FAIL 图片生成失败,请稍后重试
4003 ErrorCode.NO_IMAGE_RESULT 模型未返回图片结果,请调整图像或提示词后重试

异常类层级

所有异常均继承自 ExCodeError,可通过 except ExCodeError 统一捕获。

ExCodeError
├── ServiceError                # 通用服务错误
├── AuthenticationError         # 认证失败
├── RateLimitError              # 请求频率超限
├── QuotaExceededError          # 配额/余额不足
├── ServiceTimeoutError         # 服务超时
├── ServiceBusyError            # 服务繁忙
├── InvalidParamsError          # 请求参数无效
├── PromptBanError              # 提示词违禁
├── ImageBanError               # 图片违禁
├── ContentPolicyViolationError # 违反平台政策
├── InvalidImageError           # 图片无效或损坏
├── ImageGenerateFailError      # 图片生成失败
└── NoImageResultError          # 模型未返回图片结果

使用方式

1. 直接抛出异常

from excode import AuthenticationError

raise AuthenticationError("API Key 已过期", extra_data={"key_id": "abc123"})
# 输出: [1002] API Key 已过期 [{'key_id': 'abc123'}]

2. 根据错误码抛出(工厂函数)

from excode import ErrorCode, raise_for_error

# 使用默认描述
raise_for_error(ErrorCode.RATE_LIMIT)
# 输出: [1003] 请求频率超限,请稍后重试

# 使用自定义描述
raise_for_error(ErrorCode.RATE_LIMIT, error_msg="当前接口调用次数已达上限")
# 输出: [1003] 当前接口调用次数已达上限

3. 统一捕获并处理

from excode import ExCodeError, ErrorCode, raise_for_error

try:
    raise_for_error(ErrorCode.QUOTA_EXCEEDED)
except ExCodeError as e:
    print(e.error_code)       # ErrorCode.QUOTA_EXCEEDED
    print(e.error_code.value) # 1004
    print(e.error_msg)        # 配额/余额不足
    print(e.extra_data)       # {}

4. 分类捕获

from excode import AuthenticationError, RateLimitError, ExCodeError

try:
    # 业务逻辑
    ...
except AuthenticationError as e:
    # 处理认证失败
    refresh_api_key()
except RateLimitError as e:
    # 处理限流
    wait_and_retry()
except ExCodeError as e:
    # 兜底处理其他 excode 异常
    log_error(e)

5. 携带额外上下文

from excode import InvalidParamsError

raise InvalidParamsError(
    "参数校验失败",
    extra_data={"field": "width", "value": -1, "reason": "必须为正整数"}
)
# 输出: [2001] 参数校验失败 [{'field': 'width', 'value': -1, 'reason': '必须为正整数'}]

6. 特殊异常的扩展属性

from excode import PromptBanError, InvalidImageError

# PromptBanError 携带命中的违禁词列表
raise PromptBanError("提示词违禁", data=["暴力", "色情"])

# InvalidImageError 携带图片尺寸和原始异常
raise InvalidImageError("图片无法解码", image_size=2048, original_error=decode_err)

7. 获取错误码描述

from excode import ErrorCode, get_error_message

msg = get_error_message(ErrorCode.SERVICE_TIMEOUT)
print(msg)  # 服务超时,请稍后重试

8. 注册自定义错误码映射

from enum import IntEnum
from excode import ExCodeError, ErrorCode, raise_for_error, register_error_code

# 定义自定义异常
class NetworkError(ExCodeError):
    error_code = ErrorCode.UNKNOWN_ERROR  # 可复用或自行扩展

# 注册映射
register_error_code(ErrorCode.SERVICE_ERROR, NetworkError)

# 之后 raise_for_error 会使用你注册的异常类
raise_for_error(ErrorCode.SERVICE_ERROR, error_msg="网络连接失败")

9. 自定义错误码(CustomErrorCode)

当内置 ErrorCode 枚举无法满足需求时,可使用 CustomErrorCode 定义字符串类型的错误码:

from excode import CustomErrorCode, ServiceResult, raise_for_error

# 直接创建
MY_ERROR = CustomErrorCode("MY_ERROR", "自定义错误消息")

# 用于 ServiceResult
result = ServiceResult.fail(MY_ERROR)
print(result.code)  # CustomErrorCode('MY_ERROR', '自定义错误消息')
print(result.msg)   # 自定义错误消息

# 用于 raise_for_error(将抛出 ExCodeError)
raise_for_error(MY_ERROR)

10. 错误码注册表(ErrorCodeRegistry)

提供全局注册和查找自定义错误码的能力:

from excode import ErrorCodeRegistry

# 注册自定义错误码
MY_ERROR = ErrorCodeRegistry.register("MY_ERROR", "自定义错误消息")

# 查找已注册的错误码
found = ErrorCodeRegistry.get("MY_ERROR")
print(found)  # CustomErrorCode('MY_ERROR', '自定义错误消息')

# 列出所有已注册的错误码
all_codes = ErrorCodeRegistry.list_all()

# 清空注册表(主要用于测试)
ErrorCodeRegistry.clear()

11. 服务结果封装(ServiceResult)

使用 ServiceResult 以返回值代替异常,统一表示成功或失败:

from excode import ServiceResult, ErrorCode, CustomErrorCode

# 成功
result = ServiceResult.success(data={"id": 1, "name": "test"})
print(result.is_success)  # True
print(result.data)        # {'id': 1, 'name': 'test'}

# 失败 - 使用内置错误码
result = ServiceResult.fail(ErrorCode.AUTHENTICATION_ERROR)
print(result.is_success)  # False
print(result.code)        # ErrorCode.AUTHENTICATION_ERROR
print(result.msg)         # 认证失败,API Key 无效或过期

# 失败 - 使用自定义错误码
my_error = CustomErrorCode("BIZ_ERROR", "业务错误")
result = ServiceResult.fail(my_error)
print(result.code)  # CustomErrorCode('BIZ_ERROR', '业务错误')

# 布尔判断
if result:
    print("成功")
else:
    print("失败")

# 转为字典
d = result.to_dict()
# {'is_success': False, 'code': 'BIZ_ERROR', 'msg': '业务错误', 'data': None}

# 从异常创建
try:
    ...
except Exception as e:
    result = ServiceResult.from_exception(e)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

excode-0.1.4-cp314-cp314-win_amd64.whl (148.1 kB view details)

Uploaded CPython 3.14Windows x86-64

excode-0.1.4-cp314-cp314-manylinux_2_17_x86_64.whl (826.8 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

excode-0.1.4-cp313-cp313-win_amd64.whl (144.1 kB view details)

Uploaded CPython 3.13Windows x86-64

excode-0.1.4-cp313-cp313-manylinux_2_17_x86_64.whl (834.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

excode-0.1.4-cp312-cp312-win_amd64.whl (147.7 kB view details)

Uploaded CPython 3.12Windows x86-64

excode-0.1.4-cp312-cp312-manylinux_2_17_x86_64.whl (878.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

excode-0.1.4-cp311-cp311-win_amd64.whl (150.2 kB view details)

Uploaded CPython 3.11Windows x86-64

excode-0.1.4-cp311-cp311-manylinux_2_17_x86_64.whl (823.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

excode-0.1.4-cp310-cp310-win_amd64.whl (151.2 kB view details)

Uploaded CPython 3.10Windows x86-64

excode-0.1.4-cp310-cp310-manylinux_2_17_x86_64.whl (779.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

excode-0.1.4-cp39-cp39-win_amd64.whl (151.7 kB view details)

Uploaded CPython 3.9Windows x86-64

excode-0.1.4-cp39-cp39-manylinux_2_17_x86_64.whl (774.1 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

excode-0.1.4-cp38-cp38-win_amd64.whl (155.3 kB view details)

Uploaded CPython 3.8Windows x86-64

excode-0.1.4-cp38-cp38-manylinux_2_17_x86_64.whl (808.3 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file excode-0.1.4-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: excode-0.1.4-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 148.1 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for excode-0.1.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e1f465586787790b6395206a4cf9259be0a25e497a6f88dd2f0555391d084dea
MD5 919585310e9ea3119a5c1478cb866747
BLAKE2b-256 e922c894d14a08d6fc57abd34d40099667ab492f87c39a607a4f27ba418e72c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp314-cp314-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for excode-0.1.4-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 7db2050abbf4b0c29f8baa9cf35b942ae72bc6a6d21d46289ce6287d438a1ba0
MD5 08d1c4a3d6a689e254e88cecc77edf3e
BLAKE2b-256 1c7ba3ea30370257f58693d491ad130c98e975ac2c2518a33fe4db21e482970c

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp314-cp314-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: excode-0.1.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 144.1 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for excode-0.1.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6e7e82f48212bb813633f0269e6d151c75e31b72c090727b01ad9b8481dc6bdc
MD5 c77ea724a0f2551162524ef8f69ca541
BLAKE2b-256 8cfbe39457954c42ff82f13af25a69fc5b9eef00fd3d6c1eb3837cfd21e25de4

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp313-cp313-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for excode-0.1.4-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 7d6245c26e53544e2fa29858cdaedf64a084b862fd96c10f15c53d8d450015e6
MD5 241d26cce0ecebce0a30413384f4895e
BLAKE2b-256 a5ab713e7f35e1840b5aa5970472f97e426c438a5f1f8d4045cfc2f1723d9042

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp313-cp313-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: excode-0.1.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 147.7 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for excode-0.1.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2240288cbded9bc3001d149768944bef8553783e7b52a8cbaf188436619baa32
MD5 b71c8735d128637cfcbdd54871ba04cd
BLAKE2b-256 6b1de004e4743896eca9cdceafdc339eb6499e78eae276f4748eeaa21370f1fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp312-cp312-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for excode-0.1.4-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 f3c99d79451fb0f3d5034af5e5a71e724f87827fb9ffb55d63dc23da0adc868d
MD5 b26a412bcac7ee11630425126fe9bbea
BLAKE2b-256 c21fb95a530322444217ea1989be7b4651c6fcc6a4901d9d0458b60464291427

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp312-cp312-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: excode-0.1.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 150.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for excode-0.1.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c45e2bae59e2fc5d42f5a87a12037eaee8ba9b6e51d24dd61f2498a508984e15
MD5 6aed06119760289b0022f65bf220f849
BLAKE2b-256 1c763ac5a20b2871c3e0daec8e4eb686fb7d43a1eda00ae52041a9e1d0262eb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp311-cp311-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for excode-0.1.4-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3c506ebdb96c6a2ae0f35d1279cec793173a6366881f14bfe1e323589686553a
MD5 09a56cce94c6f735d4678fb47011a143
BLAKE2b-256 1b794be1fdc14d2959241585e93f15186bcb5ecf86b373079a621588cba47887

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp311-cp311-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: excode-0.1.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 151.2 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for excode-0.1.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d67a4dc5d20a33436b73b23617d0041cbb8baf2340593b90b41367a6f58a9801
MD5 20c6fc4eb8b489f1408a6edd4d42209e
BLAKE2b-256 8017568deaea4901ee9510b895694625f2fdbdcebfac93dbbb5b87a4b88ec68b

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp310-cp310-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for excode-0.1.4-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 706944787f177b40e02a52e3b3194b6ec5cea984d622f0bacfa2299c79a38900
MD5 494565bba9e908d4a930bd598575c8e7
BLAKE2b-256 5e8cddf0e58bf60eeec90d0d794cdb878ded6c79dc5a0594987a9694b730a5ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp310-cp310-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: excode-0.1.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 151.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for excode-0.1.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 055f34b35f951c9f2fd0f2955a06b229156b74270b8a484e08244a65d8c2e9f2
MD5 4ae6bf006f4065903147565e6cce5fee
BLAKE2b-256 3619b68fd519f5a3718b2b549f0ce6efb24ab14fb07d89d3ee53fdb00eb94b9a

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp39-cp39-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for excode-0.1.4-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 f545e9bd93bb7739398ed6ea9c302ce6643add87b7a657dccc97d1592546091a
MD5 2e61ca91853b8640f84bcc1b9ffe862f
BLAKE2b-256 911bff4b2c365bea6af82657db5435f973521282a0c540cac39b0471dc9b230f

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp39-cp39-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: excode-0.1.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 155.3 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for excode-0.1.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 da44062794dae20322ee1c1d5dafeaa0b2ca7350de07c37577d0dcb4a862841a
MD5 6a803a40cb138a001dd3de1b49bf9736
BLAKE2b-256 5341af744c5e43ed83814393bf2c9eb1e24fe80487668d8ff146ada546ff56c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp38-cp38-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file excode-0.1.4-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for excode-0.1.4-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c3098552651938097c97a3a72b12c94202ee0913ad06152c036b15c89b47229c
MD5 7d0f9b89112168958ea8e08569cee3fa
BLAKE2b-256 e30149271d7920ba63252bedca51baeb491a890a68c2d66ad74f01513e878a79

See more details on using hashes here.

Provenance

The following attestation bundles were made for excode-0.1.4-cp38-cp38-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/excode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page