Documentation
¶
Index ¶
- Variables
- func As(err error, target interface{}) bool
- func ExampleContextErrorHandling()
- func ExampleErrorHandlerRegistry()
- func ExampleErrorHandling()
- func ExampleErrorRetry()
- func ExamplePanicRecovery()
- func GetContext(err error) map[string]interface{}
- func GetGoroutineID() uint64
- func GetRetryInfo(err error) (bool, int, time.Duration)
- func GetStack(err error) string
- func Is(err error, target error) bool
- func IsHandled(err error) bool
- func IsRetriable(err error) bool
- func IsType(err error, errorType ErrorType) bool
- func MarkHandled(err error, handlerName string) error
- func SafeExec(f func() error) (err error)
- func SafeExecWithContext(ctx context.Context, f func(context.Context) error) (err error)
- func SafeGo(f func())
- func SafeGoWithContext(ctx context.Context, f func(context.Context))
- func WrapIfErr(err error, errorType ErrorType, code string, message string) error
- type AppError
- func (e *AppError) Error() string
- func (e *AppError) IsCritical() bool
- func (e *AppError) IsPermanent() bool
- func (e *AppError) IsRetriable() bool
- func (e *AppError) IsTemporary() bool
- func (e *AppError) Unwrap() error
- func (e *AppError) WithContext(key string, value interface{}) *AppError
- func (e *AppError) WithHandled(handlerName string) *AppError
- func (e *AppError) WithRetry(maxRetries int, retryDelay time.Duration) *AppError
- type ErrorHandler
- type ErrorHandlerChain
- type ErrorHandlerRegistry
- func (r *ErrorHandlerRegistry) GetHandler(errorType ErrorType) (ErrorHandler, bool)
- func (r *ErrorHandlerRegistry) Handle(err error) error
- func (r *ErrorHandlerRegistry) HandleWithContext(ctx context.Context, err error) error
- func (r *ErrorHandlerRegistry) RegisterHandler(errorType ErrorType, handler ErrorHandler)
- type ErrorType
- type LogErrorHandler
- type LogRecoveryHandler
- type PanicErrorHandler
- type RecoveryHandler
- type RecoveryHandlerChain
- type RecoveryManager
- func (m *RecoveryManager) GetStats() RecoveryStats
- func (m *RecoveryManager) HandlePanic(p interface{}) error
- func (m *RecoveryManager) SafeExec(f func() error) (err error)
- func (m *RecoveryManager) SafeExecWithContext(ctx context.Context, f func(context.Context) error) (err error)
- func (m *RecoveryManager) SafeGo(f func())
- func (m *RecoveryManager) SafeGoWithContext(ctx context.Context, f func(context.Context))
- type RecoveryStats
- type RetryErrorHandler
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = New(ErrorTypeNotFound, "NOT_FOUND", "Resource not found") ErrInvalidInput = New(ErrorTypeValidation, "INVALID_INPUT", "Invalid input") ErrForbidden = New(ErrorTypePermission, "FORBIDDEN", "Forbidden access") ErrInternal = New(ErrorTypeInternal, "INTERNAL", "Internal server error") ErrTimeout = New(ErrorTypeTemporary, "TIMEOUT", "Operation timed out") ErrAlreadyExists = New(ErrorTypePermanent, "ALREADY_EXISTS", "Resource already exists") )
预定义错误
Functions ¶
func ExampleContextErrorHandling ¶
func ExampleContextErrorHandling()
ExampleContextErrorHandling 展示带上下文的错误处理
func ExampleErrorHandlerRegistry ¶
func ExampleErrorHandlerRegistry()
ExampleErrorHandlerRegistry 展示错误处理器注册表的用法
func SafeExecWithContext ¶
SafeExecWithContext 安全地执行带上下文的函数
func SafeGoWithContext ¶
SafeGoWithContext 安全地启动带上下文的goroutine
Types ¶
type AppError ¶
type AppError struct {
Type ErrorType // 错误类型
Code string // 错误代码
Message string // 错误消息
Cause error // 原始错误
Context map[string]interface{} // 错误上下文
Stack string // 堆栈跟踪
Time time.Time // 错误发生时间
Retriable bool // 是否可重试
MaxRetries int // 最大重试次数
RetryDelay time.Duration // 重试延迟
Handled bool // 是否已处理
HandlerName string // 处理器名称
}
AppError 表示应用程序错误
func (*AppError) WithContext ¶
WithContext 添加上下文信息
func (*AppError) WithHandled ¶
WithHandled 标记为已处理
type ErrorHandler ¶
type ErrorHandler interface {
// Handle 处理错误
Handle(err error) error
// Name 返回处理器名称
Name() string
}
ErrorHandler 错误处理器接口
func DefaultErrorHandler ¶
func DefaultErrorHandler(logger hclog.Logger) ErrorHandler
DefaultErrorHandler 默认错误处理器
type ErrorHandlerChain ¶
type ErrorHandlerChain struct {
// contains filtered or unexported fields
}
ErrorHandlerChain 错误处理器链
func NewErrorHandlerChain ¶
func NewErrorHandlerChain(handlers ...ErrorHandler) *ErrorHandlerChain
NewErrorHandlerChain 创建一个新的错误处理器链
func (*ErrorHandlerChain) AddHandler ¶
func (c *ErrorHandlerChain) AddHandler(handler ErrorHandler)
AddHandler 添加处理器
type ErrorHandlerRegistry ¶
type ErrorHandlerRegistry struct {
// contains filtered or unexported fields
}
ErrorHandlerRegistry 错误处理器注册表
func DefaultErrorHandlerRegistry ¶
func DefaultErrorHandlerRegistry(logger hclog.Logger) *ErrorHandlerRegistry
DefaultErrorHandlerRegistry 默认错误处理器注册表
func NewErrorHandlerRegistry ¶
func NewErrorHandlerRegistry(logger hclog.Logger) *ErrorHandlerRegistry
NewErrorHandlerRegistry 创建一个新的错误处理器注册表
func (*ErrorHandlerRegistry) GetHandler ¶
func (r *ErrorHandlerRegistry) GetHandler(errorType ErrorType) (ErrorHandler, bool)
GetHandler 获取处理器
func (*ErrorHandlerRegistry) Handle ¶
func (r *ErrorHandlerRegistry) Handle(err error) error
Handle 处理错误
func (*ErrorHandlerRegistry) HandleWithContext ¶
func (r *ErrorHandlerRegistry) HandleWithContext(ctx context.Context, err error) error
HandleWithContext 使用上下文处理错误
func (*ErrorHandlerRegistry) RegisterHandler ¶
func (r *ErrorHandlerRegistry) RegisterHandler(errorType ErrorType, handler ErrorHandler)
RegisterHandler 注册处理器
type ErrorType ¶
type ErrorType int
ErrorType 表示错误类型
const ( ErrorTypeTemporary ErrorType = iota // 临时错误,可以重试 ErrorTypePermanent // 永久错误,不应重试 ErrorTypeCritical // 严重错误,需要立即处理 ErrorTypeValidation // 验证错误,输入数据无效 ErrorTypeNotFound // 未找到错误,请求的资源不存在 ErrorTypePermission // 权限错误,没有足够的权限 ErrorTypeInternal // 内部错误,系统内部错误 ErrorTypeExternal // 外部错误,外部系统错误 )
预定义错误类型
type LogErrorHandler ¶
type LogErrorHandler struct {
// contains filtered or unexported fields
}
LogErrorHandler 日志错误处理器
func NewLogErrorHandler ¶
func NewLogErrorHandler(logger hclog.Logger) *LogErrorHandler
NewLogErrorHandler 创建一个新的日志错误处理器
type LogRecoveryHandler ¶
type LogRecoveryHandler struct {
// contains filtered or unexported fields
}
LogRecoveryHandler 日志恢复处理器
func NewLogRecoveryHandler ¶
func NewLogRecoveryHandler(logger hclog.Logger) *LogRecoveryHandler
NewLogRecoveryHandler 创建一个新的日志恢复处理器
func (*LogRecoveryHandler) HandlePanic ¶
func (h *LogRecoveryHandler) HandlePanic(p interface{}) error
HandlePanic 处理panic
type PanicErrorHandler ¶
type PanicErrorHandler struct {
// contains filtered or unexported fields
}
PanicErrorHandler Panic错误处理器
func NewPanicErrorHandler ¶
func NewPanicErrorHandler(logger hclog.Logger) *PanicErrorHandler
NewPanicErrorHandler 创建一个新的Panic错误处理器
type RecoveryHandler ¶
type RecoveryHandler interface {
// HandlePanic 处理panic
HandlePanic(p interface{}) error
// Name 返回处理器名称
Name() string
}
RecoveryHandler 恢复处理器接口
func DefaultRecoveryHandler ¶
func DefaultRecoveryHandler(logger hclog.Logger) RecoveryHandler
DefaultRecoveryHandler 默认恢复处理器
type RecoveryHandlerChain ¶
type RecoveryHandlerChain struct {
// contains filtered or unexported fields
}
RecoveryHandlerChain 恢复处理器链
func NewRecoveryHandlerChain ¶
func NewRecoveryHandlerChain(handlers ...RecoveryHandler) *RecoveryHandlerChain
NewRecoveryHandlerChain 创建一个新的恢复处理器链
func (*RecoveryHandlerChain) AddHandler ¶
func (c *RecoveryHandlerChain) AddHandler(handler RecoveryHandler)
AddHandler 添加处理器
func (*RecoveryHandlerChain) HandlePanic ¶
func (c *RecoveryHandlerChain) HandlePanic(p interface{}) error
HandlePanic 处理panic
type RecoveryManager ¶
type RecoveryManager struct {
// contains filtered or unexported fields
}
RecoveryManager 恢复管理器
func DefaultRecoveryManager ¶
func DefaultRecoveryManager(logger hclog.Logger) *RecoveryManager
DefaultRecoveryManager 默认恢复管理器
func NewRecoveryManager ¶
func NewRecoveryManager(logger hclog.Logger, handler RecoveryHandler) *RecoveryManager
NewRecoveryManager 创建一个新的恢复管理器
func (*RecoveryManager) GetStats ¶
func (m *RecoveryManager) GetStats() RecoveryStats
GetStats 获取统计信息
func (*RecoveryManager) HandlePanic ¶
func (m *RecoveryManager) HandlePanic(p interface{}) error
HandlePanic 处理panic
func (*RecoveryManager) SafeExec ¶
func (m *RecoveryManager) SafeExec(f func() error) (err error)
SafeExec 安全地执行函数
func (*RecoveryManager) SafeExecWithContext ¶
func (m *RecoveryManager) SafeExecWithContext(ctx context.Context, f func(context.Context) error) (err error)
SafeExecWithContext 安全地执行带上下文的函数
func (*RecoveryManager) SafeGoWithContext ¶
func (m *RecoveryManager) SafeGoWithContext(ctx context.Context, f func(context.Context))
SafeGoWithContext 安全地启动带上下文的goroutine
type RecoveryStats ¶
RecoveryStats 恢复统计信息
type RetryErrorHandler ¶
type RetryErrorHandler struct {
// contains filtered or unexported fields
}
RetryErrorHandler 重试错误处理器
func NewRetryErrorHandler ¶
func NewRetryErrorHandler(maxRetries int, backoff time.Duration) *RetryErrorHandler
NewRetryErrorHandler 创建一个新的重试错误处理器