errors

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound      = New(ErrorTypeNotFound, "NOT_FOUND", "Resource not found")
	ErrInvalidInput  = New(ErrorTypeValidation, "INVALID_INPUT", "Invalid input")
	ErrUnauthorized  = New(ErrorTypePermission, "UNAUTHORIZED", "Unauthorized access")
	ErrForbidden     = New(ErrorTypePermission, "FORBIDDEN", "Forbidden access")
	ErrInternal      = New(ErrorTypeInternal, "INTERNAL", "Internal server error")
	ErrTimeout       = New(ErrorTypeTemporary, "TIMEOUT", "Operation timed out")
	ErrUnavailable   = New(ErrorTypeTemporary, "UNAVAILABLE", "Service unavailable")
	ErrAlreadyExists = New(ErrorTypePermanent, "ALREADY_EXISTS", "Resource already exists")
)

预定义错误

Functions

func As

func As(err error, target interface{}) bool

As 将错误转换为指定类型

func ExampleContextErrorHandling

func ExampleContextErrorHandling()

ExampleContextErrorHandling 展示带上下文的错误处理

func ExampleErrorHandlerRegistry

func ExampleErrorHandlerRegistry()

ExampleErrorHandlerRegistry 展示错误处理器注册表的用法

func ExampleErrorHandling

func ExampleErrorHandling()

ExampleErrorHandling 展示错误处理的基本用法

func ExampleErrorRetry

func ExampleErrorRetry()

ExampleErrorRetry 展示错误重试的用法

func ExamplePanicRecovery

func ExamplePanicRecovery()

ExamplePanicRecovery 展示Panic恢复的用法

func GetContext

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

GetContext 获取错误上下文

func GetGoroutineID

func GetGoroutineID() uint64

GetGoroutineID 获取当前goroutine的ID

func GetRetryInfo

func GetRetryInfo(err error) (bool, int, time.Duration)

GetRetryInfo 获取重试信息

func GetStack

func GetStack(err error) string

GetStack 获取错误堆栈

func Is

func Is(err error, target error) bool

Is 检查错误是否为指定类型

func IsHandled

func IsHandled(err error) bool

IsHandled 检查错误是否已处理

func IsRetriable

func IsRetriable(err error) bool

IsRetriable 检查错误是否可重试

func IsType

func IsType(err error, errorType ErrorType) bool

IsType 检查错误是否为指定类型

func MarkHandled

func MarkHandled(err error, handlerName string) error

MarkHandled 标记错误为已处理

func SafeExec

func SafeExec(f func() error) (err error)

SafeExec 安全地执行函数

func SafeExecWithContext

func SafeExecWithContext(ctx context.Context, f func(context.Context) error) (err error)

SafeExecWithContext 安全地执行带上下文的函数

func SafeGo

func SafeGo(f func())

SafeGo 安全地启动goroutine

func SafeGoWithContext

func SafeGoWithContext(ctx context.Context, f func(context.Context))

SafeGoWithContext 安全地启动带上下文的goroutine

func WrapIfErr

func WrapIfErr(err error, errorType ErrorType, code string, message string) error

WrapIfErr 如果err不为nil,则包装错误

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 New

func New(errorType ErrorType, code string, message string) *AppError

New 创建一个新的应用程序错误

func Wrap

func Wrap(err error, errorType ErrorType, code string, message string) *AppError

Wrap 包装一个错误

func (*AppError) Error

func (e *AppError) Error() string

Error 实现error接口

func (*AppError) IsCritical

func (e *AppError) IsCritical() bool

IsCritical 检查错误是否为严重错误

func (*AppError) IsPermanent

func (e *AppError) IsPermanent() bool

IsPermanent 检查错误是否为永久错误

func (*AppError) IsRetriable

func (e *AppError) IsRetriable() bool

IsRetriable 检查错误是否可重试

func (*AppError) IsTemporary

func (e *AppError) IsTemporary() bool

IsTemporary 检查错误是否为临时错误

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap 实现errors.Unwrap接口

func (*AppError) WithContext

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

WithContext 添加上下文信息

func (*AppError) WithHandled

func (e *AppError) WithHandled(handlerName string) *AppError

WithHandled 标记为已处理

func (*AppError) WithRetry

func (e *AppError) WithRetry(maxRetries int, retryDelay time.Duration) *AppError

WithRetry 设置重试信息

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 添加处理器

func (*ErrorHandlerChain) Handle

func (c *ErrorHandlerChain) Handle(err error) error

Handle 处理错误

func (*ErrorHandlerChain) Name

func (c *ErrorHandlerChain) Name() string

Name 返回处理器名称

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                    // 外部错误,外部系统错误
)

预定义错误类型

func (ErrorType) String

func (et ErrorType) String() string

String 返回错误类型的字符串表示

type LogErrorHandler

type LogErrorHandler struct {
	// contains filtered or unexported fields
}

LogErrorHandler 日志错误处理器

func NewLogErrorHandler

func NewLogErrorHandler(logger hclog.Logger) *LogErrorHandler

NewLogErrorHandler 创建一个新的日志错误处理器

func (*LogErrorHandler) Handle

func (h *LogErrorHandler) Handle(err error) error

Handle 处理错误

func (*LogErrorHandler) Name

func (h *LogErrorHandler) Name() string

Name 返回处理器名称

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

func (*LogRecoveryHandler) Name

func (h *LogRecoveryHandler) Name() string

Name 返回处理器名称

type PanicErrorHandler

type PanicErrorHandler struct {
	// contains filtered or unexported fields
}

PanicErrorHandler Panic错误处理器

func NewPanicErrorHandler

func NewPanicErrorHandler(logger hclog.Logger) *PanicErrorHandler

NewPanicErrorHandler 创建一个新的Panic错误处理器

func (*PanicErrorHandler) Handle

func (h *PanicErrorHandler) Handle(err error) error

Handle 处理错误

func (*PanicErrorHandler) Name

func (h *PanicErrorHandler) Name() string

Name 返回处理器名称

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

func (*RecoveryHandlerChain) Name

func (c *RecoveryHandlerChain) Name() string

Name 返回处理器名称

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) SafeGo

func (m *RecoveryManager) SafeGo(f func())

SafeGo 安全地启动goroutine

func (*RecoveryManager) SafeGoWithContext

func (m *RecoveryManager) SafeGoWithContext(ctx context.Context, f func(context.Context))

SafeGoWithContext 安全地启动带上下文的goroutine

type RecoveryStats

type RecoveryStats struct {
	TotalPanics     int64
	RecoveredPanics int64
	LastPanicTime   time.Time
}

RecoveryStats 恢复统计信息

type RetryErrorHandler

type RetryErrorHandler struct {
	// contains filtered or unexported fields
}

RetryErrorHandler 重试错误处理器

func NewRetryErrorHandler

func NewRetryErrorHandler(maxRetries int, backoff time.Duration) *RetryErrorHandler

NewRetryErrorHandler 创建一个新的重试错误处理器

func (*RetryErrorHandler) Handle

func (h *RetryErrorHandler) Handle(err error) error

Handle 处理错误

func (*RetryErrorHandler) Name

func (h *RetryErrorHandler) Name() string

Name 返回处理器名称

Jump to

Keyboard shortcuts

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