Documentation
¶
Index ¶
- Variables
- func GetContext(err error) map[string]interface{}
- func GetStack(err error) string
- func Is(err error, code ErrorCode) bool
- func IsConflict(err error) bool
- func IsDatabaseError(err error) bool
- func IsExternalServiceError(err error) bool
- func IsForbidden(err error) bool
- func IsInternalServer(err error) bool
- func IsInvalidParam(err error) bool
- func IsNotFound(err error) bool
- func IsTooManyRequests(err error) bool
- func IsUnauthorized(err error) bool
- func Unwrap(err error) error
- type Error
- func Internal(message ...string) *Error
- func InvalidParam(message ...string) *Error
- func New(code ErrorCode, message ...string) *Error
- func NewWithDetails(code ErrorCode, message string, details string) *Error
- func Newf(code ErrorCode, format string, args ...interface{}) *Error
- func NotFound(message ...string) *Error
- func Wrap(err error, code ErrorCode, message ...string) *Error
- func WrapWithDetails(err error, code ErrorCode, message string, details string) *Error
- func Wrapf(err error, code ErrorCode, format string, args ...interface{}) *Error
- func (e *Error) Error() string
- func (e *Error) GetMessage() string
- func (e *Error) Unwrap() error
- func (e *Error) WithContext(key string, value interface{}) *Error
- func (e *Error) WithDetails(details string) *Error
- func (e *Error) WithMessage(message string) *Error
- func (e *Error) WithStack() *Error
- type ErrorCode
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // 系统级错误码 (1000-1999) CodeInternalServer = ErrorCode{ Code: 1000, Name: "INTERNAL_SERVER_ERROR", DefaultMessage: "内部服务器错误", } CodeInvalidParam = ErrorCode{ Code: 1001, Name: "INVALID_PARAM", DefaultMessage: "参数无效", } CodeNotFound = ErrorCode{ Code: 1002, Name: "NOT_FOUND", DefaultMessage: "资源不存在", } Code: 1003, Name: "UNAUTHORIZED", DefaultMessage: "未授权", } CodeForbidden = ErrorCode{ Code: 1004, Name: "FORBIDDEN", DefaultMessage: "访问被禁止", } CodeConflict = ErrorCode{ Code: 1005, Name: "CONFLICT", DefaultMessage: "资源冲突", } CodeTooManyRequests = ErrorCode{ Code: 1006, Name: "TOO_MANY_REQUESTS", DefaultMessage: "请求过多", } // 业务级错误码 (2000-2999) CodeUserNotFound = ErrorCode{ Code: 2000, Name: "USER_NOT_FOUND", DefaultMessage: "用户不存在", } CodeUserExists = ErrorCode{ Code: 2001, Name: "USER_EXISTS", DefaultMessage: "用户已存在", } CodeInvalidPassword = ErrorCode{ Code: 2002, Name: "INVALID_PASSWORD", DefaultMessage: "密码无效", } CodeTokenExpired = ErrorCode{ Code: 2003, Name: "TOKEN_EXPIRED", DefaultMessage: "令牌已过期", } CodeTokenInvalid = ErrorCode{ Code: 2004, Name: "TOKEN_INVALID", DefaultMessage: "令牌无效", } // 数据库错误码 (3000-3999) CodeDatabaseError = ErrorCode{ Code: 3000, Name: "DATABASE_ERROR", DefaultMessage: "数据库错误", } CodeRecordNotFound = ErrorCode{ Code: 3001, Name: "RECORD_NOT_FOUND", DefaultMessage: "记录不存在", } CodeDuplicateKey = ErrorCode{ Code: 3002, Name: "DUPLICATE_KEY", DefaultMessage: "数据重复", } CodeForeignKeyViolation = ErrorCode{ Code: 3003, Name: "FOREIGN_KEY_VIOLATION", DefaultMessage: "外键约束违反", } // 外部服务错误码 (4000-4999) CodeExternalServiceError = ErrorCode{ Code: 4000, Name: "EXTERNAL_SERVICE_ERROR", DefaultMessage: "外部服务错误", } CodeNetworkError = ErrorCode{ Code: 4001, Name: "NETWORK_ERROR", DefaultMessage: "网络错误", } CodeTimeoutError = ErrorCode{ Code: 4002, Name: "TIMEOUT_ERROR", DefaultMessage: "请求超时", } )
预定义错误码变量
View Source
var ( As = stderrors.As StdIs = stderrors.Is // 重命名避免冲突 Join = stderrors.Join StdUnwrap = stderrors.Unwrap // 重命名避免冲突 ErrUnsupported = stderrors.ErrUnsupported )
重新导出标准库函数
Functions ¶
func IsExternalServiceError ¶
IsExternalServiceError 检查是否为外部服务错误
Types ¶
type Error ¶
type Error struct {
Code ErrorCode `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
Context map[string]interface{} `json:"context,omitempty"`
Stack string `json:"stack,omitempty"`
Cause error `json:"-"`
}
Error 自定义错误结构
func NewWithDetails ¶
NewWithDetails 创建带详细信息的错误
func WrapWithDetails ¶
WrapWithDetails 包装现有错误并添加详细信息
func (*Error) WithContext ¶
WithContext 添加上下文信息
type ErrorCode ¶
type ErrorCode struct {
Code int `json:"code"` // 数字错误码,用于API响应
Name string `json:"name,omitempty"` // 字符串名称,用于调试和日志
DefaultMessage string `json:"-"` // 默认消息,不序列化到JSON
}
ErrorCode 错误码设计
func NewErrorCode ¶
NewErrorCode 创建自定义错误码
func StringToCodeWithFound ¶
StringToCodeWithFound 根据字符串名称查找错误码,并返回是否找到
func (ErrorCode) GetDefaultMessage ¶
GetDefaultMessage 获取默认消息
func (ErrorCode) MarshalJSON ¶
MarshalJSON 自定义JSON序列化
func (*ErrorCode) UnmarshalJSON ¶
UnmarshalJSON 自定义JSON反序列化
Click to show internal directories.
Click to hide internal directories.