errors

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMessage

func GetMessage(err error) string

GetMessage returns the error message

func GetUserMessage

func GetUserMessage(err error) string

GetUserMessage returns a user-friendly error message

func Is

func Is(err error, code ErrorCode) bool

Is checks if error is of specific type

func IsFatal

func IsFatal(err error) bool

IsFatal checks if an error is fatal (should stop operation)

func IsRetryable

func IsRetryable(err error) bool

IsRetryable checks if an error is retryable

Types

type AppError

type AppError struct {
	Code       ErrorCode
	Message    string
	Err        error
	StackTrace string
	Context    map[string]any
}

AppError represents a structured application error

func AlreadyExists

func AlreadyExists(what string) *AppError

func InvalidConfig

func InvalidConfig(msg string) *AppError

func InvalidInput

func InvalidInput(msg string) *AppError

Common error constructors

func MemoryOperationFailed

func MemoryOperationFailed(op string, err error) *AppError

func New

func New(code ErrorCode, message string) *AppError

New creates a new application error

func NotFound

func NotFound(what string) *AppError

func ProviderUnavailable

func ProviderUnavailable(provider string) *AppError

func SessionNotFound

func SessionNotFound(id string) *AppError

func SkillNotFound

func SkillNotFound(name string) *AppError

func Timeout

func Timeout(operation string) *AppError

func ToolExecutionFailed

func ToolExecutionFailed(name string, err error) *AppError

func ToolNotFound

func ToolNotFound(name string) *AppError

func Wrap

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

Wrap wraps an existing error with code and message

func Wrapf

func Wrapf(err error, code ErrorCode, format string, args ...any) *AppError

Wrapf wraps an error with formatted message

func (*AppError) Error

func (e *AppError) Error() string

Error implements the error interface

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap returns the wrapped error

func (*AppError) WithContext

func (e *AppError) WithContext(key string, value any) *AppError

WithContext adds context to the error

type ErrorClassifier

type ErrorClassifier interface {
	ClassifyError(err error) FailoverReason
	IsFailoverError(err error) bool
}

ErrorClassifier 错误分类器接口

type ErrorCode

type ErrorCode string

ErrorCode represents a unique error code

const (
	// General errors
	ErrCodeUnknown         ErrorCode = "UNKNOWN"
	ErrCodeInvalidInput    ErrorCode = "INVALID_INPUT"
	ErrCodeInvalidConfig   ErrorCode = "INVALID_CONFIG"
	ErrCodeNotFound        ErrorCode = "NOT_FOUND"
	ErrCodeAlreadyExists   ErrorCode = "ALREADY_EXISTS"
	ErrCodePermission      ErrorCode = "PERMISSION_DENIED"
	ErrCodeTimeout         ErrorCode = "TIMEOUT"
	ErrCodeRateLimit       ErrorCode = "RATE_LIMIT"
	ErrCodeAuth            ErrorCode = "AUTHENTICATION_FAILED"
	ErrCodeBilling         ErrorCode = "BILLING_ERROR"
	ErrCodeContextOverflow ErrorCode = "CONTEXT_OVERFLOW"

	// Agent errors
	ErrCodeAgentNotRunning  ErrorCode = "AGENT_NOT_RUNNING"
	ErrCodeAgentStartFailed ErrorCode = "AGENT_START_FAILED"
	ErrCodeAgentStopFailed  ErrorCode = "AGENT_STOP_FAILED"
	ErrCodeToolExecution    ErrorCode = "TOOL_EXECUTION_FAILED"
	ErrCodeToolNotFound     ErrorCode = "TOOL_NOT_FOUND"
	ErrCodeSkillNotFound    ErrorCode = "SKILL_NOT_FOUND"
	ErrCodeSkillLoadFailed  ErrorCode = "SKILL_LOAD_FAILED"

	// Provider errors
	ErrCodeProviderUnavailable ErrorCode = "PROVIDER_UNAVAILABLE"
	ErrCodeProviderTimeout     ErrorCode = "PROVIDER_TIMEOUT"
	ErrCodeProviderError       ErrorCode = "PROVIDER_ERROR"
	ErrCodeProviderResponse    ErrorCode = "PROVIDER_RESPONSE_ERROR"

	// Channel errors
	ErrCodeChannelNotConfigured ErrorCode = "CHANNEL_NOT_CONFIGURED"
	ErrCodeChannelSendFailed    ErrorCode = "CHANNEL_SEND_FAILED"
	ErrCodeChannelReceive       ErrorCode = "CHANNEL_RECEIVE_ERROR"

	// Memory errors
	ErrCodeMemoryNotFound     ErrorCode = "MEMORY_NOT_FOUND"
	ErrCodeMemorySaveFailed   ErrorCode = "MEMORY_SAVE_FAILED"
	ErrCodeMemoryLoadFailed   ErrorCode = "MEMORY_LOAD_FAILED"
	ErrCodeMemorySearchFailed ErrorCode = "MEMORY_SEARCH_FAILED"

	// Session errors
	ErrCodeSessionNotFound  ErrorCode = "SESSION_NOT_FOUND"
	ErrCodeSessionCreate    ErrorCode = "SESSION_CREATE_FAILED"
	ErrCodeSessionSave      ErrorCode = "SESSION_SAVE_FAILED"
	ErrCodeSessionCorrupted ErrorCode = "SESSION_CORRUPTED"
)

func GetCode

func GetCode(err error) ErrorCode

GetCode returns the error code from an error if it's an AppError

type ErrorHandler

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

ErrorHandler provides centralized error handling

func NewErrorHandler

func NewErrorHandler() *ErrorHandler

NewErrorHandler creates a new error handler

func (*ErrorHandler) Handle

func (h *ErrorHandler) Handle(err error)

Handle handles an error with appropriate logging based on severity

func (*ErrorHandler) HandleWithFields

func (h *ErrorHandler) HandleWithFields(err error, fields ...zap.Field)

HandleWithFields handles an error with additional fields

func (*ErrorHandler) Handlef

func (h *ErrorHandler) Handlef(err error, format string, args ...any)

Handlef handles an error with formatted message

func (*ErrorHandler) Recover

func (h *ErrorHandler) Recover(operation string) error

Recover handles panics and converts to errors

func (*ErrorHandler) Validate

func (h *ErrorHandler) Validate(err error, code ErrorCode, message string) error

Validate checks if error is nil, returns wrapped error if not

type FailoverReason

type FailoverReason string

FailoverReason 失败原因类型

const (
	// FailoverReasonAuth 认证错误
	FailoverReasonAuth FailoverReason = "auth"
	// FailoverReasonRateLimit 速率限制
	FailoverReasonRateLimit FailoverReason = "rate_limit"
	// FailoverReasonTimeout 超时
	FailoverReasonTimeout FailoverReason = "timeout"
	// FailoverReasonBilling 计费错误
	FailoverReasonBilling FailoverReason = "billing"
	// FailoverReasonContextOverflow 上下文溢出
	FailoverReasonContextOverflow FailoverReason = "context_overflow"
	// FailoverReasonUnknown 未知错误
	FailoverReasonUnknown FailoverReason = "unknown"
)

type SimpleErrorClassifier

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

SimpleErrorClassifier 简单的错误分类器实现

func NewSimpleErrorClassifier

func NewSimpleErrorClassifier() *SimpleErrorClassifier

NewSimpleErrorClassifier 创建简单错误分类器

func (*SimpleErrorClassifier) ClassifyError

func (c *SimpleErrorClassifier) ClassifyError(err error) FailoverReason

ClassifyError 分类错误

func (*SimpleErrorClassifier) IsFailoverError

func (c *SimpleErrorClassifier) IsFailoverError(err error) bool

IsFailoverError 检查是否为可回退的错误

Jump to

Keyboard shortcuts

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