errors

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

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: "资源不存在",
	}
	CodeUnauthorized = ErrorCode{
		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 GetContext

func GetContext(err error) map[string]interface{}

GetContext 获取错误上下文

func GetStack

func GetStack(err error) string

GetStack 获取堆栈信息

func Is

func Is(err error, code ErrorCode) bool

Is 检查错误类型

func IsConflict

func IsConflict(err error) bool

IsConflict 检查是否为冲突错误

func IsDatabaseError

func IsDatabaseError(err error) bool

IsDatabaseError 检查是否为数据库错误

func IsExternalServiceError

func IsExternalServiceError(err error) bool

IsExternalServiceError 检查是否为外部服务错误

func IsForbidden

func IsForbidden(err error) bool

IsForbidden 检查是否为禁止访问错误

func IsInternalServer

func IsInternalServer(err error) bool

IsInternalServer 检查是否为内部服务器错误

func IsInvalidParam

func IsInvalidParam(err error) bool

IsInvalidParam 检查是否为无效参数错误

func IsNotFound

func IsNotFound(err error) bool

IsNotFound 检查是否为未找到错误

func IsTooManyRequests

func IsTooManyRequests(err error) bool

IsTooManyRequests 检查是否为请求过多错误

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized 检查是否为未授权错误

func Unwrap

func Unwrap(err error) error

Unwrap 解包错误

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 Internal

func Internal(message ...string) *Error

Internal 创建内部服务器错误

func InvalidParam

func InvalidParam(message ...string) *Error

InvalidParam 创建无效参数错误

func New

func New(code ErrorCode, message ...string) *Error

New 创建新的错误

func NewWithDetails

func NewWithDetails(code ErrorCode, message string, details string) *Error

NewWithDetails 创建带详细信息的错误

func Newf

func Newf(code ErrorCode, format string, args ...interface{}) *Error

Newf 创建格式化的错误

func NotFound

func NotFound(message ...string) *Error

NotFound 创建未找到错误

func Wrap

func Wrap(err error, code ErrorCode, message ...string) *Error

Wrap 包装现有错误

func WrapWithDetails

func WrapWithDetails(err error, code ErrorCode, message string, details string) *Error

WrapWithDetails 包装现有错误并添加详细信息

func Wrapf

func Wrapf(err error, code ErrorCode, format string, args ...interface{}) *Error

Wrapf 包装现有错误并格式化消息

func (*Error) Error

func (e *Error) Error() string

Error 实现error接口

func (*Error) GetMessage

func (e *Error) GetMessage() string

GetMessage 获取实际的错误消息

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap 返回原始错误

func (*Error) WithContext

func (e *Error) WithContext(key string, value interface{}) *Error

WithContext 添加上下文信息

func (*Error) WithDetails

func (e *Error) WithDetails(details string) *Error

WithDetails 添加详细信息

func (*Error) WithMessage

func (e *Error) WithMessage(message string) *Error

WithMessage 设置自定义消息

func (*Error) WithStack

func (e *Error) WithStack() *Error

WithStack 添加堆栈信息

type ErrorCode

type ErrorCode struct {
	Code           int    `json:"code"`           // 数字错误码,用于API响应
	Name           string `json:"name,omitempty"` // 字符串名称,用于调试和日志
	DefaultMessage string `json:"-"`              // 默认消息,不序列化到JSON
}

ErrorCode 错误码设计

func GetCode

func GetCode(err error) ErrorCode

GetCode 获取错误码

func NewErrorCode

func NewErrorCode(code int, name string, defaultMessage ...string) ErrorCode

NewErrorCode 创建自定义错误码

func StringToCode

func StringToCode(s string) ErrorCode

StringToCode 根据字符串名称查找错误码

func StringToCodeWithFound

func StringToCodeWithFound(s string) (ErrorCode, bool)

StringToCodeWithFound 根据字符串名称查找错误码,并返回是否找到

func (ErrorCode) Equal

func (ec ErrorCode) Equal(other ErrorCode) bool

Equal 比较两个错误码

func (ErrorCode) GetDefaultMessage

func (ec ErrorCode) GetDefaultMessage() string

GetDefaultMessage 获取默认消息

func (ErrorCode) MarshalJSON

func (ec ErrorCode) MarshalJSON() ([]byte, error)

MarshalJSON 自定义JSON序列化

func (ErrorCode) String

func (ec ErrorCode) String() string

String 返回字符串表示

func (*ErrorCode) UnmarshalJSON

func (ec *ErrorCode) UnmarshalJSON(data []byte) error

UnmarshalJSON 自定义JSON反序列化

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL