Handle errors and logging following project conventions. Use when implementing exception handling, adding logs, or when the user asks for error handling patterns with ApiException.
按项目规范实现错误处理与日志记录。适用于 API 异常、业务错误、日志打点,便于排查问题。
python
from tkms.exception.api import ApiException
python
if not user_id:
raise ApiException(code=400, message=用户ID不能为空)
if page < 1:
raise ApiException(code=400, message=页码必须大于0)
python
user = await self.userdao.getbyid(userid)
if not user:
raise ApiException(code=404, message=用户不存在)
python
try:
result = await external_api.call()
except ConnectionError as e:
raise ApiException(code=502, message=服务暂时不可用)
except Exception as e:
# 记录详细日志,对外返回通用信息
logger.exception(调用外部服务失败)
raise ApiException(code=500, message=操作失败,请稍后重试)
python
import logging
logger = logging.getLogger(name)
成功:
json
{code: 0, message: success, data: {...}}
失败(由 ApiException 触发):
json
{code: 400, message: 用户不存在}
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 gi-error-handling-1776359102 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 gi-error-handling-1776359102 技能
skillhub install gi-error-handling-1776359102
文件大小: 2.08 KB | 发布时间: 2026-4-17 14:55