Skip to content

错误码总览(Admin API)

本文档用于前端联调时快速定位 error_code
最终以 app/errors/errors.go 中定义为准。

通用错误响应结构

json
{
  "code": 404,
  "error_code": "record_not_found",
  "message": "记录不存在",
  "trace_id": "01xxxxxxxxxxxxxxxxxxxxxxx"
}
  • code: HTTP 状态码(同时也是业务返回中的 code 字段)
  • error_code: 前端建议用于分支处理的稳定错误码
  • message: 可展示给用户的文案(支持 i18n)
  • trace_id: 链路追踪 ID(排查问题时提供给后端)

常见 HTTP 状态码映射

  • 400: 参数错误、校验失败、业务前置条件不满足
  • 401: 未登录、凭证无效
  • 403: 无权限、受保护资源禁止操作
  • 404: 资源不存在
  • 429: 请求过于频繁(如导出防重复提交)
  • 500: 服务器内部错误(可携带 operation_failed 等)

错误码分组

1) 认证与会话

error_code说明常见 HTTP
not_logged_in未登录401
unauthorized未授权401
username_or_password_error用户名或密码错误401
account_disabled账号已禁用403
login_failed登录失败400/401
login_locked登录次数过多被锁定429/403
token_not_foundToken 不存在404
token_refresh_failedToken 刷新失败401
token_id_requiredToken ID 不能为空400
token_ids_requiredToken IDs 不能为空400
invalid_token_id无效的 Token ID400
invalid_token_ids无效的 Token IDs400

2) 参数与校验

error_code说明常见 HTTP
validation_failed请求参数校验失败400
invalid_argument无效参数400
params_error参数错误400
params_required参数不能为空400
id_requiredID 不能为空400
ids_requiredIDs 不能为空400
file_required文件不能为空400
file_path_required文件路径不能为空400
code_required验证码不能为空400
user_id_required用户 ID 不能为空400
invalid_user_id无效的用户 ID400
invalid_file_type文件类型无效400

3) 资源不存在

error_code说明常见 HTTP
record_not_found通用记录不存在404
admin_not_found管理员不存在404
user_not_found用户不存在404
role_not_found角色不存在404
menu_not_found菜单不存在404
permission_not_found权限不存在404
department_not_found部门不存在404
position_not_found岗位不存在404
dictionary_not_found字典不存在404
blacklist_not_found黑名单不存在404
notification_not_found通知不存在404
order_not_found订单不存在404
payment_not_found支付记录不存在404
payment_method_not_found支付方式不存在404
attachment_not_found附件不存在404
log_not_found日志不存在404
export_record_not_found导出记录不存在404

4) 管理员/角色保护规则

error_code说明常见 HTTP
protected_admin受保护管理员不可操作403
admin_protected_cannot_disable受保护管理员不能禁用403
admin_protected_cannot_delete受保护管理员不能删除403
admin_cannot_delete_self不能删除自己403
admin_cannot_modify_roles不能修改管理员角色403
role_protected_cannot_modify_slug受保护角色不能修改标识403
role_protected_cannot_disable受保护角色不能禁用403
role_protected_cannot_delete受保护角色不能删除403

5) 2FA(谷歌验证器)

error_code说明常见 HTTP
google_authenticator_not_bound未绑定谷歌验证器400/403
google_authenticator_already_bound已绑定谷歌验证器400
google_code_required谷歌验证码不能为空400
google_code_invalid谷歌验证码无效400
secret_and_code_required密钥和验证码不能为空400

6) 业务冲突与依赖关系

error_code说明常见 HTTP
username_exists用户名已存在400
menu_slug_exists菜单标识已存在400
role_name_exists角色名称已存在400
role_slug_exists角色标识已存在400
permission_name_exists权限名称已存在400
permission_slug_exists权限标识已存在400
permission_name_or_slug_exists权限名称或标识已存在400
department_has_children部门存在子部门,无法删除400
department_has_admins部门存在管理员,无法删除400
position_has_admins岗位存在管理员,无法删除400
menu_has_children菜单存在子菜单,无法删除400

7) 订单与支付

error_code说明常见 HTTP
order_id_required订单 ID 不能为空400
generate_order_no_failed生成订单号失败500
create_order_failed创建订单失败500
create_order_detail_failed创建订单详情失败500
query_order_detail_failed查询订单详情失败500
delete_order_detail_failed删除订单详情失败500
payment_method_disabled支付方式已禁用400/403
payment_method_code_exists支付方式代码已存在400
invalid_payment_type无效支付类型400
payment_config_required支付配置不能为空400
create_payment_failed创建支付记录失败500
payment_amount_invalid支付金额无效400
payment_status_invalid支付状态无效400

8) 导入导出

error_code说明常见 HTTP
export_record_not_found导出记录不存在404
write_csv_header_failed写入 CSV 表头失败500
write_csv_data_failed写入 CSV 数据失败500
csv_write_failedCSV 写入失败500
batch_delete_export_failed批量删除导出记录失败500
invalid_csv_formatCSV 格式无效400

9) 附件与分片上传

error_code说明常见 HTTP
chunk_upload_only_local_storage分片上传仅支持本地存储400
invalid_chunk_index分片索引无效400
invalid_total_chunks总分片数无效400
invalid_total_size总大小无效400
invalid_chunk_size分片大小无效400
chunk_file_required分片文件不能为空400
invalid_action无效操作400
chunk_not_found分片不存在404
chunk_missing分片缺失400
no_chunk_data_to_merge没有可合并的分片400
save_chunk_failed保存分片失败500
create_directory_failed创建目录失败500
create_file_failed创建文件失败500
write_chunk_failed写入分片失败500
close_file_failed关闭文件失败500
save_file_failed保存文件失败500
delete_file_failed删除文件失败500

10) 通用操作错误

error_code说明常见 HTTP
create_failed创建失败500
update_failed更新失败500
delete_failed删除失败500
query_failed查询失败500
password_encrypt_failed密码加密失败500
operation_failed操作失败(兜底错误)500

前端对接建议

  • 业务分支判断优先使用 error_code,不要仅依赖 message 文案。
  • 异常排查时同时记录 trace_id,便于后端定位日志。
  • 若接口文档与实际不一致,以接口实际返回和本文件说明为准,并反馈补充。