-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexceptions.py
More file actions
43 lines (35 loc) · 1.95 KB
/
exceptions.py
File metadata and controls
43 lines (35 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from enum import Enum
# 自定义错误码
class CustomError(Enum):
"""错误码枚举类(支持中英文)"""
# ===== 基础错误码 (1000-1999) =====
SUCCESS = (0, "成功", "Success")
PARAM_VALIDATION_FAILED = (1001, "参数校验失败", "Parameter validation failed")
VIDEO_SCENE_SPLIT_TIMEOUT = (1002, "视频场景分割超时", "Video scene split timeout")
# ===== 业务错误码 (2000-2999) =====
VIDEO_SCENE_SPLIT_FAILED = (2001, "视频场景分割失败", "Failed to video scene split")
FILE_SIZE_LIMIT_EXCEEDED = (2002, "文件大小超出限制", "File size exceeds the limit")
DOWNLOAD_FILE_FAILED = (2003, "下载文件失败", "Download file failed")
INSUFFICIENT_ACCOUNT_BALANCE = (2004, "帐户余额不足,请充值", "Insufficient account balance, please recharge")
INVALID_APIKEY = (2005, "无效的apiKey", "Invalid apiKey")
DOWNLOAD_FILE_TIMEOUT = (2006, "下载文件超时,请检查资源文件下载是否正常", "Download file timeout, please check if the resource file download is normal")
# ===== 系统错误码 (9000-9999) =====
INTERNAL_SERVER_ERROR = (9998, "系统内部错误", "Internal server error")
UNKNOWN_ERROR = (9999, "未知异常", "Unknown error")
def __init__(self, code: int, cn_message: str, en_message: str):
self.code = code
self.cn_message = cn_message
self.en_message = en_message
def as_dict(self, detail: str = "", lang: str = 'zh') -> dict:
"""转换为API响应格式,支持中英文"""
message = self.cn_message if lang == 'zh' else self.en_message
if detail:
message += f": {detail}"
return {"code": self.code, "message": message}
# 自定义异常类
class CustomException(Exception):
"""自定义业务异常类"""
def __init__(self, err: CustomError, detail: str = ""):
self.err = err
self.detail = detail
super().__init__(err.cn_message)