errors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package errors provides error types and utilities for the cache package.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Cache errors
	ErrCacheClosed     = errors.New("cache is closed")
	ErrKeyNotFound     = errors.New("key not found")
	ErrInvalidKey      = errors.New("invalid key")
	ErrInvalidValue    = errors.New("invalid value")
	ErrMemoryLimit     = errors.New("memory limit exceeded")
	ErrCapacityLimit   = errors.New("capacity limit exceeded")
	ErrContextCanceled = errors.New("operation canceled by context")

	// TTL errors
	ErrInvalidTTL  = errors.New("invalid TTL value")
	ErrTTLTooShort = errors.New("TTL value is too short")
	ErrTTLTooLong  = errors.New("TTL value is too long")

	// Store errors
	ErrStoreError         = errors.New("store operation failed")
	ErrStoreConnection    = errors.New("store connection failed")
	ErrStoreTimeout       = errors.New("store operation timed out")
	ErrInvalidSize        = errors.New("max size must be greater than 0")
	ErrInvalidMemoryLimit = errors.New("max memory cannot be negative")

	// Data errors
	ErrCompression     = errors.New("compression error")
	ErrDecompression   = errors.New("decompression error")
	ErrSerialization   = errors.New("serialization error")
	ErrDeserialization = errors.New("deserialization error")

	// Operation errors
	ErrReplicationError = errors.New("replication error")
	ErrBatchOperation   = errors.New("batch operation failed")
	ErrInvalidOperation = errors.New("invalid operation")
	ErrPermissionDenied = errors.New("permission denied")
)

Common error types

Functions

func IsCacheClosed

func IsCacheClosed(err error) bool

IsCacheClosed checks if the error is a cache closed error

func IsCacheError

func IsCacheError(err error) bool

IsCacheError checks if an error is a CacheError

func IsContextCanceled

func IsContextCanceled(err error) bool

IsContextCanceled checks if the error is a context canceled error

func IsErrorType

func IsErrorType(err error, errType ErrorType) bool

IsErrorType checks if an error is of a specific type

func IsKeyNotFound

func IsKeyNotFound(err error) bool

IsKeyNotFound checks if the error is a key not found error

func NewCacheError

func NewCacheError(errType ErrorType, op string, key any, err error) error

NewCacheError creates a new CacheError

func RecoverFromPanic

func RecoverFromPanic(op string, key any) bool

RecoverFromPanic recovers from a panic and updates metrics

func ResetErrorMetrics

func ResetErrorMetrics()

ResetErrorMetrics resets all error metrics

func WrapError

func WrapError(op string, key any, err error) error

WrapError wraps an error with context and updates metrics

Types

type CacheError

type CacheError struct {
	Op      string
	Key     any
	Err     error
	ErrType ErrorType
}

CacheError represents a cache operation error

func GetCacheError

func GetCacheError(err error) *CacheError

GetCacheError returns the CacheError if the error is a CacheError

func (*CacheError) Error

func (e *CacheError) Error() string

Error implements the error interface

func (*CacheError) Is

func (e *CacheError) Is(target error) bool

Is reports whether the target error is of the same type as the receiver

func (*CacheError) Unwrap

func (e *CacheError) Unwrap() error

Unwrap returns the underlying error

type ErrorMetrics

type ErrorMetrics struct {
	// Error counts by type
	CacheErrors      atomic.Int64
	StoreErrors      atomic.Int64
	ValidationErrors atomic.Int64
	OperationErrors  atomic.Int64

	// Last error timestamps
	LastCacheError      atomic.Value // time.Time
	LastStoreError      atomic.Value // time.Time
	LastValidationError atomic.Value // time.Time
	LastOperationError  atomic.Value // time.Time

	// Error recovery stats
	PanicRecoveries atomic.Int64
	LastPanic       atomic.Value // time.Time
}

ErrorMetrics tracks error statistics

func GetErrorMetrics

func GetErrorMetrics() *ErrorMetrics

GetErrorMetrics returns the current error metrics

type ErrorType

type ErrorType string

ErrorType represents the category of error

const (
	// ErrorTypeCache represents cache-specific errors
	ErrorTypeCache ErrorType = "cache"
	// ErrorTypeStore represents storage backend errors
	ErrorTypeStore ErrorType = "store"
	// ErrorTypeValidation represents validation errors
	ErrorTypeValidation ErrorType = "validation"
	// ErrorTypeOperation represents operation-specific errors
	ErrorTypeOperation ErrorType = "operation"
)

Jump to

Keyboard shortcuts

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