Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-02-15 09:19:15 * @FilePath: \go-toolbox\pkg\errorx\base.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
Index ¶
- Constants
- func Assert(condition bool, message string) error
- func Is(err, target error) bool
- func IsConcurrentOperationError(err error) bool
- func IsEventProcessingFailedError(err error) bool
- func IsHandlerNotFoundError(err error) bool
- func IsHandlerPanicError(err error) bool
- func IsHandlerTimeoutError(err error) bool
- func IsInvalidFilterError(err error) bool
- func IsInvalidHandlerError(err error) bool
- func IsInvalidMiddlewareError(err error) bool
- func IsInvalidParamError(err error) bool
- func IsInvalidStateError(err error) bool
- func IsNetworkError(err error) bool
- func IsNotFoundError(err error) bool
- func IsQueueFullError(err error) bool
- func IsResourceExhaustedError(err error) bool
- func IsTimeoutError(err error) bool
- func IsType(err error, errType ErrorType) bool
- func Must(err error)
- func MustNot(condition bool, message string)
- func New(message string) error
- func NewAlreadyExistsError(resource string) error
- func NewConcurrentOperationError(operation string) error
- func NewConfigError(config string) error
- func NewConfigInvalidError(config string) error
- func NewConfigMissingError(config string) error
- func NewConflictError(resource string) error
- func NewConnectionLostError(target string) error
- func NewConnectionTimeoutError(target string) error
- func NewDataCorruptedError(data string) error
- func NewDataNotFoundError(data string) error
- func NewDuplicateDataError(data string) error
- func NewEventProcessingFailedError(event string) error
- func NewForbiddenError(reason string) error
- func NewHandlerNotFoundError(handler string) error
- func NewHandlerPanicError(handler string) error
- func NewHandlerTimeoutError(handler string) error
- func NewInternalError(message string) error
- func NewInvalidFilterError(filter string) error
- func NewInvalidFormatError(format string) error
- func NewInvalidHandlerError(handler string) error
- func NewInvalidMiddlewareError(middleware string) error
- func NewInvalidParamError(param string) error
- func NewInvalidStateError(state string) error
- func NewMissingParamError(param string) error
- func NewNetworkError(message string) error
- func NewNotFoundError(resource string) error
- func NewNotImplementedError(feature string) error
- func NewQueueFullError(queue string) error
- func NewResourceExhaustedError(resource string) error
- func NewTimeoutError(operation string) error
- func NewTypedError(errType ErrorType, msg string, args ...interface{}) error
- func NewUnauthorizedError(reason string) error
- func NewUnavailableError(service string) error
- func Newf(format string, args ...interface{}) error
- func PrintErrorMap()
- func Recover(fn func()) (err error)
- func RegisterError(errType ErrorType, msg string)
- func ResetErrorMap()
- func Unwrap(err error) error
- func Wrap(err error, message string) error
- func WrapError(message string, err ...error) error
- type BaseError
- type CustomError
- type ErrorChain
- func (c *ErrorChain) AddError(err error) *ErrorChain
- func (c *ErrorChain) AddErrorWithContext(err error, context map[string]interface{}) *ErrorChain
- func (c *ErrorChain) Error() string
- func (c *ErrorChain) GetErrors() []ErrorInfo
- func (c *ErrorChain) GetLastError() *ErrorInfo
- func (c *ErrorChain) HasErrors() bool
- func (c *ErrorChain) String() string
- type ErrorCollector
- type ErrorInfo
- type ErrorMapType
- type ErrorType
- type ErrorWithStack
- type RetryableError
- type ValidationError
- type ValidationErrors
Constants ¶
const ( ErrTypeUnknownError ErrorType = -1 // 参数错误 ErrTypeInvalidParam = iota + 1000 ErrTypeMissingParam ErrTypeInvalidFormat // 业务错误 ErrTypeNotFound ErrTypeAlreadyExists ErrTypeConflict ErrTypeForbidden // 系统错误 ErrTypeInternal ErrTypeTimeout ErrTypeResourceExhausted ErrTypeNotImplemented // 网络错误 ErrTypeNetworkError ErrTypeConnectionLost ErrTypeConnectionTimeout // 数据错误 ErrTypeDataCorrupted ErrTypeDataNotFound ErrTypeDuplicateData // 配置错误 ErrTypeConfigError ErrTypeConfigMissing ErrTypeConfigInvalid // 状态错误 ErrTypeInvalidState ErrTypeConcurrentOperation // 事件错误 ErrTypeHandlerPanic ErrTypeHandlerNotFound ErrTypeQueueFull ErrTypeHandlerTimeout ErrTypeInvalidHandler ErrTypeInvalidFilter ErrTypeInvalidMiddleware ErrTypeEventProcessingFailed )
预定义的错误类型
Variables ¶
This section is empty.
Functions ¶
func IsHandlerNotFoundError ¶
func IsHandlerTimeoutError ¶
func IsInvalidFilterError ¶
func IsInvalidHandlerError ¶
func IsNetworkError ¶
func IsNotFoundError ¶
func IsQueueFullError ¶
func IsTimeoutError ¶
func NewAlreadyExistsError ¶
func NewConfigInvalidError ¶
func NewConfigMissingError ¶
func NewConflictError ¶
func NewConnectionLostError ¶
func NewDataNotFoundError ¶
func NewDuplicateDataError ¶
func NewForbiddenError ¶
func NewHandlerNotFoundError ¶
func NewHandlerTimeoutError ¶
func NewInvalidFilterError ¶
func NewInvalidFormatError ¶
func NewInvalidHandlerError ¶
func NewMissingParamError ¶
func NewNotImplementedError ¶
func NewQueueFullError ¶
func NewTimeoutError ¶
func NewTypedError ¶
NewTypedError 创建统一的类型化错误,支持模板格式化文案
func NewUnauthorizedError ¶
func NewUnavailableError ¶
Types ¶
type CustomError ¶
CustomError 自定义错误结构,包含错误码和详细信息
func NewCustomError ¶
func NewCustomError(code ErrorType, message string, details map[string]interface{}) *CustomError
NewCustomError 创建自定义错误
func (*CustomError) GetDetails ¶
func (e *CustomError) GetDetails() map[string]interface{}
GetDetails 获取错误详情
type ErrorChain ¶
type ErrorChain struct {
// contains filtered or unexported fields
}
ErrorChain 错误链,支持错误追踪
func (*ErrorChain) AddErrorWithContext ¶
func (c *ErrorChain) AddErrorWithContext(err error, context map[string]interface{}) *ErrorChain
AddErrorWithContext 添加带上下文的错误
func (*ErrorChain) GetLastError ¶
func (c *ErrorChain) GetLastError() *ErrorInfo
GetLastError 获取最后一个错误
type ErrorCollector ¶
type ErrorCollector struct {
// contains filtered or unexported fields
}
批量错误处理
func NewErrorCollector ¶
func NewErrorCollector() *ErrorCollector
func (*ErrorCollector) Add ¶
func (c *ErrorCollector) Add(err error)
func (*ErrorCollector) Error ¶
func (c *ErrorCollector) Error() string
func (*ErrorCollector) GetErrors ¶
func (c *ErrorCollector) GetErrors() []error
func (*ErrorCollector) HasErrors ¶
func (c *ErrorCollector) HasErrors() bool
type ErrorInfo ¶
type ErrorInfo struct {
Error error
Timestamp time.Time
Location string
Context map[string]interface{}
}
ErrorInfo 错误信息
type ErrorMapType ¶
错误映射类型
type ErrorType ¶
type ErrorType int
错误类型常量
func ClassifyError ¶
ClassifyError 获取错误的 ErrorType 如果错误是 BaseError 类型,返回其错误类型;否则返回 ErrTypeUnknownError
type ErrorWithStack ¶
ErrorWithStack 带调用栈的错误
func NewErrorWithStack ¶
func NewErrorWithStack(message string) *ErrorWithStack
NewErrorWithStack 创建带调用栈的错误
func (*ErrorWithStack) GetStackTrace ¶
func (e *ErrorWithStack) GetStackTrace() string
GetStackTrace 获取调用栈信息
type RetryableError ¶
type RetryableError struct {
BaseError
MaxRetries int
CurrentRetry int
RetryAfter time.Duration
Retryable bool
}
RetryableError 可重试的错误
func NewRetryableError ¶
func NewRetryableError(message string, maxRetries int, retryAfter time.Duration) *RetryableError
NewRetryableError 创建可重试的错误
func (*RetryableError) GetRetryAfter ¶
func (e *RetryableError) GetRetryAfter() time.Duration
GetRetryAfter 获取重试间隔
func (*RetryableError) IncrementRetry ¶
func (e *RetryableError) IncrementRetry()
IncrementRetry 增加重试次数
func (*RetryableError) ShouldRetry ¶
func (e *RetryableError) ShouldRetry() bool
ShouldRetry 检查是否应该重试
type ValidationError ¶
type ValidationError struct {
BaseError
Field string
Value interface{}
Rule string
Details map[string]interface{}
}
ValidationError 验证错误
func NewValidationError ¶
func NewValidationError(field, rule string, value interface{}, message string) *ValidationError
NewValidationError 创建验证错误
func (*ValidationError) AddDetail ¶
func (e *ValidationError) AddDetail(key string, value interface{})
AddDetail 添加详细信息
type ValidationErrors ¶
type ValidationErrors struct {
Errors []*ValidationError
}
ValidationErrors 多个验证错误
func NewValidationErrors ¶
func NewValidationErrors() *ValidationErrors
NewValidationErrors 创建多个验证错误
func (*ValidationErrors) GetFieldErrors ¶
func (ve *ValidationErrors) GetFieldErrors(field string) []*ValidationError
GetFieldErrors 获取特定字段的错误