This commit is contained in:
@@ -28,6 +28,7 @@ type AppError struct {
|
||||
Code int // 应用错误码
|
||||
Message string // 错误消息
|
||||
Err error // 底层错误(可选)
|
||||
Data any // 可安全返回的结构化错误上下文(可选)
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
@@ -59,6 +60,13 @@ func New(code int, customMsg ...string) *AppError {
|
||||
}
|
||||
}
|
||||
|
||||
// NewWithData 创建携带安全结构化上下文的 AppError。
|
||||
func NewWithData(code int, data any, customMsg ...string) *AppError {
|
||||
err := New(code, customMsg...)
|
||||
err.Data = data
|
||||
return err
|
||||
}
|
||||
|
||||
// Wrap 用错误码和消息包装现有错误
|
||||
// 优先使用 errorMessages 映射表中的消息,允许通过可选参数覆盖
|
||||
// 用法:
|
||||
|
||||
Reference in New Issue
Block a user