Files
junhong_cmp_fiber/pkg/errors/errors.go
huang 194078674a
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m45s
feat: 实现单卡资产分配与回收功能
- 新增单卡分配/回收 API(支持 ICCID 列表、号段范围、筛选条件三种选卡方式)
- 新增资产分配记录查询 API(支持多条件筛选和分页)
- 新增 AssetAllocationRecord 模型、Store、Service、Handler 完整实现
- 扩展 IotCardStore 新增批量更新、号段查询、筛选查询等方法
- 修复 GORM Callback 处理 slice 类型(BatchCreate)的问题
- 新增完整的单元测试和集成测试
- 同步 OpenSpec 规范并归档 change
2026-01-24 15:46:15 +08:00

78 lines
2.7 KiB
Go

package errors
import (
"errors"
"fmt"
)
// 中间件标准错误类型
var (
ErrMissingToken = errors.New("missing authentication token")
ErrInvalidToken = errors.New("invalid or expired token")
ErrRedisUnavailable = errors.New("redis unavailable")
ErrTooManyRequests = errors.New("too many requests")
)
// 预定义业务错误(常用错误可直接引用)
var (
ErrAssetAllocationRecordNotFound = &AppError{Code: CodeAssetAllocationRecordNotFound, Message: "分配记录不存在"}
ErrNotDirectSubordinate = &AppError{Code: CodeNotDirectSubordinate, Message: "只能操作直属下级店铺"}
ErrIotCardBoundToDevice = &AppError{Code: CodeIotCardBoundToDevice, Message: "IoT 卡已绑定设备,不能单独操作"}
ErrIotCardStatusNotAllowed = &AppError{Code: CodeIotCardStatusNotAllowed, Message: "卡状态不允许此操作"}
ErrCannotAllocateToSelf = &AppError{Code: CodeCannotAllocateToSelf, Message: "不能分配给自己"}
ErrCannotRecallFromSelf = &AppError{Code: CodeCannotRecallFromSelf, Message: "不能从自己回收"}
)
// AppError 表示带错误码的应用错误
type AppError struct {
Code int // 应用错误码
Message string // 错误消息
Err error // 底层错误(可选)
}
func (e *AppError) Error() string {
if e.Err != nil {
return fmt.Sprintf("%s: %v", e.Message, e.Err)
}
return e.Message
}
func (e *AppError) Unwrap() error {
return e.Err
}
// New 创建新的 AppError
// 优先使用 errorMessages 映射表中的消息,允许通过可选参数覆盖
// 用法:
// - errors.New(errors.CodeNotFound) // 使用映射表默认消息
// - errors.New(errors.CodeNotFound, "提现申请不存在") // 覆盖为自定义消息
func New(code int, customMsg ...string) *AppError {
// 默认从映射表获取消息
message := GetMessage(code, "zh-CN")
// 如果提供了自定义消息且非空,则覆盖
if len(customMsg) > 0 && customMsg[0] != "" {
message = customMsg[0]
}
return &AppError{
Code: code,
Message: message,
}
}
// Wrap 用错误码和消息包装现有错误
// 优先使用 errorMessages 映射表中的消息,允许通过可选参数覆盖
// 用法:
// - errors.Wrap(errors.CodeDatabaseError, originalErr) // 使用映射表默认消息
// - errors.Wrap(errors.CodeDatabaseError, originalErr, "查询用户失败") // 覆盖为自定义消息
func Wrap(code int, err error, customMsg ...string) *AppError {
message := GetMessage(code, "zh-CN")
if len(customMsg) > 0 && customMsg[0] != "" {
message = customMsg[0]
}
return &AppError{
Code: code,
Message: message,
Err: err,
}
}