Documentation
¶
Overview ¶
Package errors provides a comprehensive error handling framework for AINative Code. It includes custom error types, error wrapping/unwrapping, stack traces, and recovery strategies.
Example (AuthenticationError) ¶
Example demonstrates authentication error
package main
import (
"fmt"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
err := errors.NewExpiredTokenError("google")
fmt.Println("Error:", err.Error())
fmt.Println("Provider:", err.Provider)
fmt.Println("Can retry:", err.IsRetryable())
fmt.Println("User message:", err.UserMessage())
}
Output: Error: [AUTH_EXPIRED_TOKEN] Authentication token expired for provider 'google' Provider: google Can retry: true User message: Authentication error: Your session has expired. Please log in again.
Example (BasicUsage) ¶
Example demonstrates basic error creation and handling
package main
import (
"fmt"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
// Create a configuration error
err := errors.NewConfigMissingError("api_key")
// Get technical error message (for logging)
fmt.Println("Technical:", err.Error())
// Get user-friendly message (for UI display)
fmt.Println("User:", err.UserMessage())
// Check error properties
fmt.Println("Code:", err.Code())
fmt.Println("Severity:", err.Severity())
fmt.Println("Retryable:", err.IsRetryable())
}
Output: Technical: [CONFIG_MISSING] Required configuration 'api_key' is missing User: Configuration error: Required setting 'api_key' is not configured. Please check your configuration file. Code: CONFIG_MISSING Severity: high Retryable: false
Example (CircuitBreaker) ¶
Example demonstrates circuit breaker pattern
package main
import (
"fmt"
"time"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
// Create circuit breaker (max 2 failures, 100ms reset)
cb := errors.NewCircuitBreaker(2, 100*time.Millisecond)
// Simulate failing operation
for i := 0; i < 3; i++ {
err := cb.Execute(func() error {
return fmt.Errorf("service unavailable")
})
if err != nil {
if cb.GetState() == errors.StateOpen {
fmt.Println("Circuit opened")
break
}
}
}
// Circuit is now open
err := cb.Execute(func() error {
return nil
})
if err != nil {
fmt.Println("Request blocked:", err.Error())
}
}
Output: Circuit opened Request blocked: circuit breaker is open: too many failures
Example (DatabaseError) ¶
Example demonstrates database error handling
package main
import (
"fmt"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
// Not found error
notFoundErr := errors.NewDBNotFoundError("users", "id=123")
fmt.Println("Not found:", notFoundErr.UserMessage())
// Duplicate entry error
dupErr := errors.NewDBDuplicateError("users", "email", "test@example.com")
fmt.Println("Duplicate:", dupErr.UserMessage())
}
Output: Not found: Not found: The requested resource does not exist. Duplicate: Duplicate entry: A record with this email already exists.
Example (ErrorCodes) ¶
Example demonstrates error code extraction
package main
import (
"fmt"
"time"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
configErr := errors.NewConfigInvalidError("timeout", "must be positive")
authErr := errors.NewAuthFailedError("provider", nil)
providerErr := errors.NewProviderRateLimitError("openai", 60*time.Second)
fmt.Println("Config:", errors.GetCode(configErr))
fmt.Println("Auth:", errors.GetCode(authErr))
fmt.Println("Provider:", errors.GetCode(providerErr))
}
Output: Config: CONFIG_INVALID Auth: AUTH_FAILED Provider: PROVIDER_RATE_LIMIT
Example (ErrorSeverity) ¶
Example demonstrates error severity checking
package main
import (
"fmt"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
criticalErr := errors.NewDBConnectionError("postgres", fmt.Errorf("refused"))
highErr := errors.NewConfigMissingError("database_url")
mediumErr := errors.NewDBDuplicateError("users", "email", "test@example.com")
lowErr := errors.NewDBNotFoundError("products", "id=999")
fmt.Println("Critical:", errors.GetSeverity(criticalErr))
fmt.Println("High:", errors.GetSeverity(highErr))
fmt.Println("Medium:", errors.GetSeverity(mediumErr))
fmt.Println("Low:", errors.GetSeverity(lowErr))
}
Output: Critical: critical High: high Medium: medium Low: low
Example (ErrorWrapping) ¶
Example demonstrates error wrapping
package main
import (
"fmt"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
// Simulate a lower-level error
dbErr := fmt.Errorf("connection refused")
// Wrap it with context
wrappedErr := errors.Wrap(dbErr, errors.ErrCodeDBConnection, "failed to connect to database")
fmt.Println(wrappedErr)
}
Output: [DB_CONNECTION_FAILED] failed to connect to database: connection refused
Example (Fallback) ¶
Example demonstrates fallback pattern
package main
import (
"fmt"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
// Try primary operation, fall back to alternative
err := errors.Fallback(
func() error {
return fmt.Errorf("primary service unavailable")
},
func() error {
fmt.Println("Using fallback service")
return nil
},
)
if err == nil {
fmt.Println("Operation succeeded")
}
}
Output: Using fallback service Operation succeeded
Example (ProviderErrorWithMetadata) ¶
Example demonstrates provider error with metadata
package main
import (
"fmt"
"time"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
err := errors.NewProviderTimeoutError("openai", "gpt-4", 30*time.Second)
err.WithMetadata("request_id", "req-123456")
err.WithMetadata("user_id", "user-789")
fmt.Println("Provider:", err.ProviderName)
fmt.Println("Model:", err.Model)
fmt.Println("Retryable:", err.IsRetryable())
metadata := err.Metadata()
fmt.Println("Request ID:", metadata["request_id"])
}
Output: Provider: openai Model: gpt-4 Retryable: true Request ID: req-123456
Example (RetryWithBackoff) ¶
Example demonstrates retry with exponential backoff
package main
import (
"context"
"fmt"
"time"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
ctx := context.Background()
// Configure retry strategy
config := errors.NewRetryConfig()
config.Strategy = errors.NewLinearBackoff(10*time.Millisecond, 3)
callCount := 0
config.OnRetry = func(attempt int, err error) {
fmt.Printf("Retry attempt %d failed\n", attempt+1)
}
// Simulate an operation that fails twice then succeeds
err := errors.Retry(ctx, func() error {
callCount++
if callCount < 3 {
return errors.NewProviderTimeoutError("api", "model", 30*time.Second)
}
return nil
}, config)
if err != nil {
fmt.Println("Failed after all retries")
} else {
fmt.Printf("Success after %d total attempts\n", callCount)
}
}
Output: Retry attempt 1 failed Retry attempt 2 failed Success after 3 total attempts
Example (ToolExecutionError) ¶
Example demonstrates tool execution error
package main
import (
"fmt"
"github.com/AINative-studio/ainative-code/internal/errors"
)
func main() {
err := errors.NewToolNotFoundError("git")
err.WithPath("/usr/bin/git")
fmt.Println("Error:", err.Error())
fmt.Println("Tool:", err.ToolName)
fmt.Println("Path:", err.ToolPath)
fmt.Println("User message:", err.UserMessage())
}
Output: Error: [TOOL_NOT_FOUND] Tool 'git' not found Tool: git Path: /usr/bin/git User message: Tool error: 'git' is not available or not installed. Please verify the tool is properly configured.
Index ¶
- Variables
- func As(err error, target interface{}) bool
- func Chain(err error) []error
- func DisableDebugMode()
- func EnableDebugMode()
- func ExampleAdvancedCircuitBreaker_usage()
- func ExampleProviderRecoveryStrategy_apiKeyResolution()
- func ExampleProviderRecoveryStrategy_basic()
- func ExampleProviderRecoveryStrategy_customConfig()
- func ExampleProviderRecoveryStrategy_fromConfig()
- func ExampleProviderRecoveryStrategy_integration()
- func ExampleProviderRecoveryStrategy_rateLimit()
- func ExampleProviderRecoveryStrategy_serverError()
- func ExampleProviderRecoveryStrategy_timeoutIncrease()
- func ExampleProviderRecoveryStrategy_tokenReduction()
- func Fallback(primary func() error, fallback func() error) error
- func FallbackWithValue[T any](primary func() (T, error), fallbackValue T) (T, error)
- func Format(err error) string
- func FormatChain(err error) string
- func FormatUser(err error) string
- func FromJSON(data []byte) error
- func Is(err, target error) bool
- func IsDebugMode() bool
- func IsRetryable(err error) bool
- func Retry(ctx context.Context, fn func() error, config *RetryConfig) error
- func RetryWithRecovery(ctx context.Context, fn func() error, config *RetryConfig, ...) error
- func RootCause(err error) error
- func ToJSON(err error) ([]byte, error)
- func Unwrap(err error) error
- func Wrap(err error, code ErrorCode, message string) error
- func Wrapf(err error, code ErrorCode, format string, args ...interface{}) error
- type AdvancedCircuitBreaker
- type AuthenticationError
- func NewAuthFailedError(provider string, cause error) *AuthenticationError
- func NewAuthenticationError(code ErrorCode, message string) *AuthenticationError
- func NewExpiredTokenError(provider string) *AuthenticationError
- func NewInvalidCredentialsError(provider string) *AuthenticationError
- func NewInvalidTokenError(provider string) *AuthenticationError
- func NewPermissionDeniedError(resource, action string) *AuthenticationError
- type BaseError
- func (e *BaseError) Code() ErrorCode
- func (e *BaseError) Error() string
- func (e *BaseError) IsRetryable() bool
- func (e *BaseError) Metadata() map[string]interface{}
- func (e *BaseError) Severity() Severity
- func (e *BaseError) Stack() []StackFrame
- func (e *BaseError) StackTrace() string
- func (e *BaseError) Unwrap() error
- func (e *BaseError) UserMessage() string
- func (e *BaseError) WithMetadata(key string, value interface{}) *BaseError
- type CircuitBreaker
- type CircuitState
- type ConfigError
- func NewConfigError(code ErrorCode, message string, configKey string) *ConfigError
- func NewConfigInvalidError(configKey, reason string) *ConfigError
- func NewConfigMissingError(configKey string) *ConfigError
- func NewConfigParseError(configPath string, cause error) *ConfigError
- func NewConfigValidationError(configKey, validationRule string) *ConfigError
- type DatabaseError
- func NewDBConnectionError(dbName string, cause error) *DatabaseError
- func NewDBConstraintError(table, constraint string, cause error) *DatabaseError
- func NewDBDuplicateError(table, field, value string) *DatabaseError
- func NewDBNotFoundError(table, identifier string) *DatabaseError
- func NewDBQueryError(operation, table string, cause error) *DatabaseError
- func NewDBTransactionError(operation string, cause error) *DatabaseError
- func NewDatabaseError(code ErrorCode, message string) *DatabaseError
- type ErrorCode
- type ErrorResponse
- type ErrorSummary
- type ExponentialBackoff
- type LinearBackoff
- type ProviderError
- func NewProviderError(code ErrorCode, message string, providerName string) *ProviderError
- func NewProviderInvalidResponseError(providerName string, reason string, cause error) *ProviderError
- func NewProviderNotFoundError(providerName string) *ProviderError
- func NewProviderRateLimitError(providerName string, retryAfter time.Duration) *ProviderError
- func NewProviderTimeoutError(providerName, model string, timeout time.Duration) *ProviderError
- func NewProviderUnavailableError(providerName string, cause error) *ProviderError
- func (e *ProviderError) GetRetryDelay() time.Duration
- func (e *ProviderError) ShouldRetry() bool
- func (e *ProviderError) WithModel(model string) *ProviderError
- func (e *ProviderError) WithRequestID(requestID string) *ProviderError
- func (e *ProviderError) WithStatusCode(statusCode int) *ProviderError
- type ProviderRecoveryStrategy
- type RecoveryAction
- type RecoveryDecision
- type RetryConfig
- type RetryStrategy
- type SecurityError
- func NewDecryptionError(reason string) *SecurityError
- func NewEncryptionError(reason string) *SecurityError
- func NewInvalidKeyError(keyType, reason string) *SecurityError
- func NewSecurityError(code ErrorCode, message string, resource string) *SecurityError
- func NewSecurityViolationError(resource, action, reason string) *SecurityError
- type Severity
- type StackFrame
- type ToolExecutionError
- func NewToolExecutionError(code ErrorCode, message string, toolName string) *ToolExecutionError
- func NewToolExecutionFailedError(toolName string, exitCode int, output string, cause error) *ToolExecutionError
- func NewToolInvalidInputError(toolName, paramName, reason string) *ToolExecutionError
- func NewToolNotFoundError(toolName string) *ToolExecutionError
- func NewToolPermissionDeniedError(toolName, resource string) *ToolExecutionError
- func NewToolTimeoutError(toolName string, timeout time.Duration) *ToolExecutionError
- func (e *ToolExecutionError) GetOutput(maxLength int) string
- func (e *ToolExecutionError) WithExitCode(exitCode int) *ToolExecutionError
- func (e *ToolExecutionError) WithOutput(output string) *ToolExecutionError
- func (e *ToolExecutionError) WithParameter(name string, value interface{}) *ToolExecutionError
- func (e *ToolExecutionError) WithPath(path string) *ToolExecutionError
Examples ¶
- Package (AuthenticationError)
- Package (BasicUsage)
- Package (CircuitBreaker)
- Package (DatabaseError)
- Package (ErrorCodes)
- Package (ErrorSeverity)
- Package (ErrorWrapping)
- Package (Fallback)
- Package (ProviderErrorWithMetadata)
- Package (RetryWithBackoff)
- Package (ToolExecutionError)
- Format
- FormatUser
- NewAuthFailedError
- NewConfigInvalidError
- NewConfigMissingError
- NewDBConnectionError
- NewDBNotFoundError
- NewPermissionDeniedError
- NewProviderRateLimitError
- NewProviderTimeoutError
- NewToolExecutionFailedError
- NewToolTimeoutError
- Wrap
- Wrap (WithConfigError)
Constants ¶
This section is empty.
Variables ¶
var DebugMode = false
DebugMode controls whether stack traces are included in error output
Functions ¶
func ExampleAdvancedCircuitBreaker_usage ¶
func ExampleAdvancedCircuitBreaker_usage()
Example: Advanced circuit breaker with error-specific thresholds
func ExampleProviderRecoveryStrategy_apiKeyResolution ¶
func ExampleProviderRecoveryStrategy_apiKeyResolution()
Example: API key re-resolution on 401 errors
func ExampleProviderRecoveryStrategy_basic ¶
func ExampleProviderRecoveryStrategy_basic()
Example: Basic usage of ProviderRecoveryStrategy
func ExampleProviderRecoveryStrategy_customConfig ¶
func ExampleProviderRecoveryStrategy_customConfig()
Example: Custom retry configuration
func ExampleProviderRecoveryStrategy_fromConfig ¶
func ExampleProviderRecoveryStrategy_fromConfig()
Example: Configuration-based setup
func ExampleProviderRecoveryStrategy_integration ¶
func ExampleProviderRecoveryStrategy_integration()
Example: Complete provider integration pattern
func ExampleProviderRecoveryStrategy_rateLimit ¶
func ExampleProviderRecoveryStrategy_rateLimit()
Example: Rate limiting with Retry-After header
func ExampleProviderRecoveryStrategy_serverError ¶
func ExampleProviderRecoveryStrategy_serverError()
Example: Server error with exponential backoff
func ExampleProviderRecoveryStrategy_timeoutIncrease ¶
func ExampleProviderRecoveryStrategy_timeoutIncrease()
Example: Timeout increase on timeout errors
func ExampleProviderRecoveryStrategy_tokenReduction ¶
func ExampleProviderRecoveryStrategy_tokenReduction()
Example: Token reduction on token limit errors
func FallbackWithValue ¶
FallbackWithValue executes a primary function and returns a fallback value on error
func Format ¶
Format formats an error for display
Example ¶
err := NewConfigMissingError("api_key")
formatted := Format(err)
println(formatted)
func FormatUser ¶
FormatUser formats an error for end-user display (friendly message)
Example ¶
err := NewAuthFailedError("openai", errors.New("invalid token"))
userMsg := FormatUser(err)
println(userMsg)
func Retry ¶
func Retry(ctx context.Context, fn func() error, config *RetryConfig) error
Retry executes a function with retry logic
func RetryWithRecovery ¶
func RetryWithRecovery(ctx context.Context, fn func() error, config *RetryConfig, recovery func(error) error) error
RetryWithRecovery executes a function with retry and recovery logic
func Wrap ¶
Wrap wraps an existing error with additional context
Example ¶
originalErr := fmt.Errorf("connection refused")
wrappedErr := Wrap(originalErr, ErrCodeDBConnection, "failed to connect to database")
fmt.Println(wrappedErr)
Output: [DB_CONNECTION_FAILED] failed to connect to database: connection refused
Example (WithConfigError) ¶
err := NewConfigInvalidError("timeout", "must be a positive integer")
fmt.Println(err.Code())
fmt.Println(err.Severity())
fmt.Println(err.IsRetryable())
Output: CONFIG_INVALID high false
Types ¶
type AdvancedCircuitBreaker ¶
type AdvancedCircuitBreaker struct {
*CircuitBreaker
// contains filtered or unexported fields
}
AdvancedCircuitBreaker extends the basic circuit breaker with error-specific behavior
func NewAdvancedCircuitBreaker ¶
func NewAdvancedCircuitBreaker(maxFailures int, resetTimeout time.Duration) *AdvancedCircuitBreaker
NewAdvancedCircuitBreaker creates an advanced circuit breaker
func (*AdvancedCircuitBreaker) ExecuteWithErrorType ¶
func (acb *AdvancedCircuitBreaker) ExecuteWithErrorType(fn func() error, errorType string) error
ExecuteWithErrorType executes a function with error-type-specific circuit breaking
func (*AdvancedCircuitBreaker) SetErrorThreshold ¶
func (acb *AdvancedCircuitBreaker) SetErrorThreshold(errorType string, threshold int)
SetErrorThreshold sets a specific threshold for an error type
type AuthenticationError ¶
AuthenticationError represents authentication and authorization errors
func NewAuthFailedError ¶
func NewAuthFailedError(provider string, cause error) *AuthenticationError
NewAuthFailedError creates an error for general authentication failures
Example ¶
originalErr := errors.New("connection timeout")
err := NewAuthFailedError("openai", originalErr)
println(err.Error())
println(err.UserMessage())
func NewAuthenticationError ¶
func NewAuthenticationError(code ErrorCode, message string) *AuthenticationError
NewAuthenticationError creates a new authentication error
func NewExpiredTokenError ¶
func NewExpiredTokenError(provider string) *AuthenticationError
NewExpiredTokenError creates an error for expired authentication tokens
func NewInvalidCredentialsError ¶
func NewInvalidCredentialsError(provider string) *AuthenticationError
NewInvalidCredentialsError creates an error for invalid credentials
func NewInvalidTokenError ¶
func NewInvalidTokenError(provider string) *AuthenticationError
NewInvalidTokenError creates an error for invalid authentication tokens
func NewPermissionDeniedError ¶
func NewPermissionDeniedError(resource, action string) *AuthenticationError
NewPermissionDeniedError creates an error for permission/authorization failures
Example ¶
err := NewPermissionDeniedError("/api/admin", "access")
println(err.Resource)
println(err.Severity())
func (*AuthenticationError) WithProvider ¶
func (e *AuthenticationError) WithProvider(provider string) *AuthenticationError
WithProvider sets the authentication provider
func (*AuthenticationError) WithResource ¶
func (e *AuthenticationError) WithResource(resource string) *AuthenticationError
WithResource sets the resource that was being accessed
func (*AuthenticationError) WithUserID ¶
func (e *AuthenticationError) WithUserID(userID string) *AuthenticationError
WithUserID sets the user ID associated with the error
type BaseError ¶
type BaseError struct {
// contains filtered or unexported fields
}
BaseError is the foundation for all custom errors in the system
func (*BaseError) IsRetryable ¶
IsRetryable returns whether the error is retryable
func (*BaseError) StackTrace ¶
StackTrace returns a formatted stack trace string
func (*BaseError) UserMessage ¶
UserMessage returns a user-friendly error message
func (*BaseError) WithMetadata ¶
WithMetadata adds metadata to the error
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements the circuit breaker pattern for fault tolerance
func NewCircuitBreaker ¶
func NewCircuitBreaker(maxFailures int, resetTimeout time.Duration) *CircuitBreaker
NewCircuitBreaker creates a new circuit breaker
func (*CircuitBreaker) Execute ¶
func (cb *CircuitBreaker) Execute(fn func() error) error
Execute runs a function through the circuit breaker
func (*CircuitBreaker) GetState ¶
func (cb *CircuitBreaker) GetState() CircuitState
GetState returns the current circuit state
func (*CircuitBreaker) Reset ¶
func (cb *CircuitBreaker) Reset()
Reset manually resets the circuit breaker
type CircuitState ¶
type CircuitState int
CircuitState represents the state of a circuit breaker
const ( // StateClosed allows requests through StateClosed CircuitState = iota // StateOpen blocks requests StateOpen // StateHalfOpen allows limited requests to test recovery StateHalfOpen )
type ConfigError ¶
ConfigError represents configuration-related errors
func NewConfigError ¶
func NewConfigError(code ErrorCode, message string, configKey string) *ConfigError
NewConfigError creates a new configuration error
func NewConfigInvalidError ¶
func NewConfigInvalidError(configKey, reason string) *ConfigError
NewConfigInvalidError creates an error for invalid configuration
Example ¶
err := NewConfigInvalidError("timeout", "must be a positive integer")
println(err.Error())
println(err.UserMessage())
func NewConfigMissingError ¶
func NewConfigMissingError(configKey string) *ConfigError
NewConfigMissingError creates an error for missing configuration
Example ¶
err := NewConfigMissingError("api_key")
println(err.ConfigKey)
println(err.Severity())
func NewConfigParseError ¶
func NewConfigParseError(configPath string, cause error) *ConfigError
NewConfigParseError creates an error for configuration parsing failures
func NewConfigValidationError ¶
func NewConfigValidationError(configKey, validationRule string) *ConfigError
NewConfigValidationError creates an error for configuration validation failures
func (*ConfigError) WithPath ¶
func (e *ConfigError) WithPath(path string) *ConfigError
WithPath adds the configuration file path to the error
type DatabaseError ¶
type DatabaseError struct {
*BaseError
Table string
Query string
Operation string
Constraint string
}
DatabaseError represents database-related errors
func NewDBConnectionError ¶
func NewDBConnectionError(dbName string, cause error) *DatabaseError
NewDBConnectionError creates an error for database connection failures
Example ¶
originalErr := errors.New("connection timeout")
err := NewDBConnectionError("postgres", originalErr)
println(err.Code())
println(err.IsRetryable())
func NewDBConstraintError ¶
func NewDBConstraintError(table, constraint string, cause error) *DatabaseError
NewDBConstraintError creates an error for constraint violations
func NewDBDuplicateError ¶
func NewDBDuplicateError(table, field, value string) *DatabaseError
NewDBDuplicateError creates an error for duplicate key violations
func NewDBNotFoundError ¶
func NewDBNotFoundError(table, identifier string) *DatabaseError
NewDBNotFoundError creates an error for record not found
Example ¶
err := NewDBNotFoundError("products", "sku=ABC123")
println(err.Error())
println(err.UserMessage())
println(err.Table)
func NewDBQueryError ¶
func NewDBQueryError(operation, table string, cause error) *DatabaseError
NewDBQueryError creates an error for query execution failures
func NewDBTransactionError ¶
func NewDBTransactionError(operation string, cause error) *DatabaseError
NewDBTransactionError creates an error for transaction failures
func NewDatabaseError ¶
func NewDatabaseError(code ErrorCode, message string) *DatabaseError
NewDatabaseError creates a new database error
func (*DatabaseError) WithConstraint ¶
func (e *DatabaseError) WithConstraint(constraint string) *DatabaseError
WithConstraint sets the constraint that was violated
func (*DatabaseError) WithOperation ¶
func (e *DatabaseError) WithOperation(operation string) *DatabaseError
WithOperation sets the database operation
func (*DatabaseError) WithQuery ¶
func (e *DatabaseError) WithQuery(query string) *DatabaseError
WithQuery sets the query that failed
func (*DatabaseError) WithTable ¶
func (e *DatabaseError) WithTable(table string) *DatabaseError
WithTable sets the database table name
type ErrorCode ¶
type ErrorCode string
ErrorCode represents a unique error code for categorization
const ( // Configuration error codes ErrCodeConfigInvalid ErrorCode = "CONFIG_INVALID" ErrCodeConfigMissing ErrorCode = "CONFIG_MISSING" ErrCodeConfigParse ErrorCode = "CONFIG_PARSE" ErrCodeConfigValidation ErrorCode = "CONFIG_VALIDATION" // Authentication error codes ErrCodeAuthFailed ErrorCode = "AUTH_FAILED" ErrCodeAuthInvalidToken ErrorCode = "AUTH_INVALID_TOKEN" ErrCodeAuthExpiredToken ErrorCode = "AUTH_EXPIRED_TOKEN" ErrCodeAuthPermissionDenied ErrorCode = "AUTH_PERMISSION_DENIED" ErrCodeAuthInvalidCredentials ErrorCode = "AUTH_INVALID_CREDENTIALS" // Provider error codes ErrCodeProviderTimeout ErrorCode = "PROVIDER_TIMEOUT" ErrCodeProviderRateLimit ErrorCode = "PROVIDER_RATE_LIMIT" ErrCodeProviderInvalidResponse ErrorCode = "PROVIDER_INVALID_RESPONSE" ErrCodeProviderNotFound ErrorCode = "PROVIDER_NOT_FOUND" // Tool execution error codes ErrCodeToolNotFound ErrorCode = "TOOL_NOT_FOUND" ErrCodeToolExecutionFailed ErrorCode = "TOOL_EXECUTION_FAILED" ErrCodeToolTimeout ErrorCode = "TOOL_TIMEOUT" ErrCodeToolInvalidInput ErrorCode = "TOOL_INVALID_INPUT" ErrCodeToolPermissionDenied ErrorCode = "TOOL_PERMISSION_DENIED" // Database error codes ErrCodeDBConnection ErrorCode = "DB_CONNECTION_FAILED" ErrCodeDBQuery ErrorCode = "DB_QUERY_FAILED" ErrCodeDBNotFound ErrorCode = "DB_NOT_FOUND" ErrCodeDBDuplicate ErrorCode = "DB_DUPLICATE" ErrCodeDBConstraint ErrorCode = "DB_CONSTRAINT_VIOLATION" ErrCodeDBTransaction ErrorCode = "DB_TRANSACTION_FAILED" // Security error codes ErrCodeSecurityViolation ErrorCode = "SECURITY_VIOLATION" ErrCodeSecurityInvalidKey ErrorCode = "SECURITY_INVALID_KEY" ErrCodeSecurityEncryption ErrorCode = "SECURITY_ENCRYPTION_FAILED" ErrCodeSecurityDecryption ErrorCode = "SECURITY_DECRYPTION_FAILED" )
type ErrorResponse ¶
type ErrorResponse struct {
Code string `json:"code"`
Message string `json:"message"`
UserMsg string `json:"user_message,omitempty"`
Severity string `json:"severity"`
Retryable bool `json:"retryable"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Stack []StackFrame `json:"stack,omitempty"`
}
ErrorResponse represents a JSON error response
type ErrorSummary ¶
type ErrorSummary struct {
Code string
Message string
Severity string
Retryable bool
HasCause bool
StackSize int
}
ErrorSummary provides a summary of an error for logging
type ExponentialBackoff ¶
type ExponentialBackoff struct {
InitialDelay time.Duration
MaxDelay time.Duration
Multiplier float64
MaxRetries int
}
ExponentialBackoff implements exponential backoff retry strategy
func NewExponentialBackoff ¶
func NewExponentialBackoff() *ExponentialBackoff
NewExponentialBackoff creates a new exponential backoff strategy with sensible defaults
func (*ExponentialBackoff) GetDelay ¶
func (e *ExponentialBackoff) GetDelay(attempt int) time.Duration
GetDelay calculates the exponential backoff delay
func (*ExponentialBackoff) MaxAttempts ¶
func (e *ExponentialBackoff) MaxAttempts() int
MaxAttempts returns the maximum number of retry attempts
func (*ExponentialBackoff) ShouldRetry ¶
func (e *ExponentialBackoff) ShouldRetry(err error, attempt int) bool
ShouldRetry determines if the error is retryable
type LinearBackoff ¶
LinearBackoff implements linear backoff retry strategy
func NewLinearBackoff ¶
func NewLinearBackoff(delay time.Duration, maxRetries int) *LinearBackoff
NewLinearBackoff creates a new linear backoff strategy
func (*LinearBackoff) GetDelay ¶
func (l *LinearBackoff) GetDelay(attempt int) time.Duration
GetDelay returns a constant delay
func (*LinearBackoff) MaxAttempts ¶
func (l *LinearBackoff) MaxAttempts() int
MaxAttempts returns the maximum number of retry attempts
func (*LinearBackoff) ShouldRetry ¶
func (l *LinearBackoff) ShouldRetry(err error, attempt int) bool
ShouldRetry determines if the error is retryable
type ProviderError ¶
type ProviderError struct {
*BaseError
ProviderName string
Model string
RequestID string
StatusCode int
RetryAfter *time.Duration
}
ProviderError represents errors related to external AI provider interactions
func NewProviderError ¶
func NewProviderError(code ErrorCode, message string, providerName string) *ProviderError
NewProviderError creates a new provider error
func NewProviderInvalidResponseError ¶
func NewProviderInvalidResponseError(providerName string, reason string, cause error) *ProviderError
NewProviderInvalidResponseError creates an error for invalid provider responses
func NewProviderNotFoundError ¶
func NewProviderNotFoundError(providerName string) *ProviderError
NewProviderNotFoundError creates an error for provider not found
func NewProviderRateLimitError ¶
func NewProviderRateLimitError(providerName string, retryAfter time.Duration) *ProviderError
NewProviderRateLimitError creates an error for rate limit exceeded
Example ¶
retryAfter := 60 * time.Second
err := NewProviderRateLimitError("openai", retryAfter)
println(err.Error())
println(err.GetRetryDelay())
println(err.ShouldRetry())
func NewProviderTimeoutError ¶
func NewProviderTimeoutError(providerName, model string, timeout time.Duration) *ProviderError
NewProviderTimeoutError creates an error for provider request timeouts
Example ¶
err := NewProviderTimeoutError("anthropic", "claude-3", 30*time.Second)
println(err.ProviderName)
println(err.Model)
println(err.IsRetryable())
func NewProviderUnavailableError ¶
func NewProviderUnavailableError(providerName string, cause error) *ProviderError
NewProviderUnavailableError creates an error for unavailable providers
func (*ProviderError) GetRetryDelay ¶
func (e *ProviderError) GetRetryDelay() time.Duration
GetRetryDelay returns the suggested retry delay
func (*ProviderError) ShouldRetry ¶
func (e *ProviderError) ShouldRetry() bool
ShouldRetry determines if the provider error should be retried
func (*ProviderError) WithModel ¶
func (e *ProviderError) WithModel(model string) *ProviderError
WithModel sets the model name
func (*ProviderError) WithRequestID ¶
func (e *ProviderError) WithRequestID(requestID string) *ProviderError
WithRequestID sets the request ID for tracking
func (*ProviderError) WithStatusCode ¶
func (e *ProviderError) WithStatusCode(statusCode int) *ProviderError
WithStatusCode sets the HTTP status code
type ProviderRecoveryStrategy ¶
type ProviderRecoveryStrategy struct {
MaxRetries int
InitialBackoff time.Duration
MaxBackoff time.Duration
Multiplier float64
EnableJitter bool
OnAPIKeyResolution func(ctx context.Context) (string, error)
OnTokenReduction func(currentTokens int) int
OnTimeoutIncrease func(currentTimeout time.Duration) time.Duration
Logger func(message string)
}
ProviderRecoveryStrategy implements sophisticated recovery strategies for provider API failures
func NewProviderRecoveryStrategy ¶
func NewProviderRecoveryStrategy() *ProviderRecoveryStrategy
NewProviderRecoveryStrategy creates a new provider recovery strategy with defaults
func (*ProviderRecoveryStrategy) AnalyzeError ¶
func (p *ProviderRecoveryStrategy) AnalyzeError(ctx context.Context, err error, attempt int, statusCode int, retryAfterHeader string) RecoveryDecision
AnalyzeError analyzes an error and returns a recovery decision
func (*ProviderRecoveryStrategy) ExecuteWithRecovery ¶
func (p *ProviderRecoveryStrategy) ExecuteWithRecovery( ctx context.Context, fn func(ctx context.Context) error, getStatusCode func() int, getRetryAfter func() string, ) error
ExecuteWithRecovery executes a function with sophisticated error recovery
type RecoveryAction ¶
type RecoveryAction int
RecoveryAction represents an action to take for error recovery
const ( // ActionRetry indicates a simple retry with backoff ActionRetry RecoveryAction = iota // ActionRetryWithBackoff indicates exponential backoff retry ActionRetryWithBackoff // ActionResolveAPIKey indicates re-resolution of API key before retry ActionResolveAPIKey // ActionReduceTokens indicates reducing max_tokens before retry ActionReduceTokens // ActionIncreaseTimeout indicates increasing timeout before retry ActionIncreaseTimeout // ActionCircuitBreak indicates circuit breaker activation ActionCircuitBreak // ActionFail indicates non-retryable error ActionFail )
type RecoveryDecision ¶
type RecoveryDecision struct {
Action RecoveryAction
RetryAfter time.Duration
ShouldRetry bool
NewAPIKey string
TokenReduction int
NewTimeout time.Duration
Message string
}
RecoveryDecision contains the decision on how to handle an error
type RetryConfig ¶
type RetryConfig struct {
Strategy RetryStrategy
OnRetry func(attempt int, err error)
OnFinalError func(err error)
}
RetryConfig holds configuration for retry operations
func NewRetryConfig ¶
func NewRetryConfig() *RetryConfig
NewRetryConfig creates a default retry configuration
type RetryStrategy ¶
type RetryStrategy interface {
// ShouldRetry determines if an error should trigger a retry
ShouldRetry(err error, attempt int) bool
// GetDelay calculates the delay before the next retry attempt
GetDelay(attempt int) time.Duration
// MaxAttempts returns the maximum number of retry attempts
MaxAttempts() int
}
RetryStrategy defines the strategy for retrying failed operations
type SecurityError ¶
SecurityError represents security-related errors
func NewDecryptionError ¶
func NewDecryptionError(reason string) *SecurityError
NewDecryptionError creates an error for decryption failures
func NewEncryptionError ¶
func NewEncryptionError(reason string) *SecurityError
NewEncryptionError creates an error for encryption failures
func NewInvalidKeyError ¶
func NewInvalidKeyError(keyType, reason string) *SecurityError
NewInvalidKeyError creates an error for invalid encryption/API keys
func NewSecurityError ¶
func NewSecurityError(code ErrorCode, message string, resource string) *SecurityError
NewSecurityError creates a new security error
func NewSecurityViolationError ¶
func NewSecurityViolationError(resource, action, reason string) *SecurityError
NewSecurityViolationError creates an error for security policy violations
func (*SecurityError) WithAction ¶
func (e *SecurityError) WithAction(action string) *SecurityError
WithAction adds the action being attempted to the error
type Severity ¶
type Severity string
Severity represents the severity level of an error
func GetSeverity ¶
GetSeverity extracts the severity from an error
type StackFrame ¶
StackFrame represents a single frame in the stack trace
type ToolExecutionError ¶
type ToolExecutionError struct {
*BaseError
ToolName string
ToolPath string
Parameters map[string]interface{}
ExitCode int
Output string
}
ToolExecutionError represents errors during tool execution
func NewToolExecutionError ¶
func NewToolExecutionError(code ErrorCode, message string, toolName string) *ToolExecutionError
NewToolExecutionError creates a new tool execution error
func NewToolExecutionFailedError ¶
func NewToolExecutionFailedError(toolName string, exitCode int, output string, cause error) *ToolExecutionError
NewToolExecutionFailedError creates an error for failed tool executions
Example ¶
originalErr := errors.New("exit status 1")
err := NewToolExecutionFailedError("git", 1, "fatal: not a git repository", originalErr)
println(err.Error())
println(err.ExitCode)
println(err.GetOutput(100))
func NewToolInvalidInputError ¶
func NewToolInvalidInputError(toolName, paramName, reason string) *ToolExecutionError
NewToolInvalidInputError creates an error for invalid tool input
func NewToolNotFoundError ¶
func NewToolNotFoundError(toolName string) *ToolExecutionError
NewToolNotFoundError creates an error for tools that cannot be found
func NewToolPermissionDeniedError ¶
func NewToolPermissionDeniedError(toolName, resource string) *ToolExecutionError
NewToolPermissionDeniedError creates an error for permission-related tool failures
func NewToolTimeoutError ¶
func NewToolTimeoutError(toolName string, timeout time.Duration) *ToolExecutionError
NewToolTimeoutError creates an error for tool execution timeouts
Example ¶
err := NewToolTimeoutError("terraform", 30*time.Second)
println(err.ToolName)
println(err.IsRetryable())
func (*ToolExecutionError) GetOutput ¶
func (e *ToolExecutionError) GetOutput(maxLength int) string
GetOutput returns the tool output, truncated if necessary
func (*ToolExecutionError) WithExitCode ¶
func (e *ToolExecutionError) WithExitCode(exitCode int) *ToolExecutionError
WithExitCode sets the exit code
func (*ToolExecutionError) WithOutput ¶
func (e *ToolExecutionError) WithOutput(output string) *ToolExecutionError
WithOutput sets the tool output
func (*ToolExecutionError) WithParameter ¶
func (e *ToolExecutionError) WithParameter(name string, value interface{}) *ToolExecutionError
WithParameter adds a parameter to the error context
func (*ToolExecutionError) WithPath ¶
func (e *ToolExecutionError) WithPath(path string) *ToolExecutionError
WithPath sets the tool path