errors

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package errors provides structured error handling for Typosentinel This package implements comprehensive error types with context and categorization

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHTTPStatus

func GetHTTPStatus(err error) int

GetHTTPStatus returns the appropriate HTTP status code for an error

func HasCode

func HasCode(err error, code ErrorCode) bool

HasCode checks if an error has a specific error code

func IsAppError

func IsAppError(err error) bool

IsAppError checks if an error is an AppError

func IsRetryable

func IsRetryable(err error) bool

IsRetryable checks if an error is retryable

Types

type AppError

type AppError struct {
	Code       ErrorCode              `json:"code"`
	Message    string                 `json:"message"`
	Cause      error                  `json:"-"`
	Context    map[string]interface{} `json:"context,omitempty"`
	Severity   Severity               `json:"severity"`
	Timestamp  time.Time              `json:"timestamp"`
	StackTrace string                 `json:"stack_trace,omitempty"`
	RequestID  string                 `json:"request_id,omitempty"`
	UserID     string                 `json:"user_id,omitempty"`
	Retryable  bool                   `json:"retryable"`
}

AppError represents a structured application error

func GetAppError

func GetAppError(err error) *AppError

GetAppError extracts an AppError from an error chain

func New

func New(code ErrorCode, message string) *AppError

New creates a new AppError

func NewAlreadyExistsError

func NewAlreadyExistsError(resource, id string) *AppError

func NewAppError

func NewAppError(code ErrorCode, message string) *AppError

NewAppError creates a new AppError with the given code and message

func NewConfigError

func NewConfigError(key, reason string) *AppError

Configuration error constructors

func NewDatabaseError

func NewDatabaseError(operation string, err error) *AppError

Database error constructors

func NewForbiddenError

func NewForbiddenError(resource string) *AppError

func NewInternalError

func NewInternalError(message string) *AppError

Internal error constructors

func NewInvalidInputError

func NewInvalidInputError(field, value string) *AppError

func NewMissingRequiredError

func NewMissingRequiredError(field string) *AppError

func NewNetworkError

func NewNetworkError(message string) *AppError

Network error constructors

func NewNotFoundError

func NewNotFoundError(resource, id string) *AppError

Resource error constructors

func NewPanicError

func NewPanicError(recovered interface{}) *AppError

func NewParsingError

func NewParsingError(format, input string) *AppError

func NewProcessingError

func NewProcessingError(message string) *AppError

Processing error constructors

func NewRateLimitError

func NewRateLimitError(limit int, window time.Duration) *AppError

func NewTimeoutError

func NewTimeoutError(operation string, timeout time.Duration) *AppError

func NewUnauthorizedError

func NewUnauthorizedError(message string) *AppError

Authentication error constructors

func NewValidationError

func NewValidationError(message string) *AppError

Validation error constructors

func Newf

func Newf(code ErrorCode, format string, args ...interface{}) *AppError

Newf creates a new AppError with formatted message

func Wrap

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

Wrap wraps an existing error with additional context

func Wrapf

func Wrapf(err error, code ErrorCode, format string, args ...interface{}) *AppError

Wrapf wraps an existing error with formatted message

func (*AppError) Error

func (e *AppError) Error() string

Error implements the error interface

func (*AppError) GetCode

func (e *AppError) GetCode() ErrorCode

GetCode returns the error code

func (*AppError) GetSeverity

func (e *AppError) GetSeverity() Severity

GetSeverity returns the error severity

func (*AppError) Is

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

Is checks if the error matches the target error

func (*AppError) IsRetryable

func (e *AppError) IsRetryable() bool

IsRetryable returns whether the error is retryable

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap returns the underlying cause error

func (*AppError) WithContext

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

WithContext adds context information to the error

func (*AppError) WithRequestID

func (e *AppError) WithRequestID(requestID string) *AppError

WithRequestID adds a request ID to the error

func (*AppError) WithUserID

func (e *AppError) WithUserID(userID string) *AppError

WithUserID adds a user ID to the error

type ErrorCode

type ErrorCode string

ErrorCode represents different categories of errors

const (
	// General errors
	INTERNAL_ERROR   ErrorCode = "INTERNAL_ERROR"
	INVALID_INPUT    ErrorCode = "INVALID_INPUT"
	UNAUTHORIZED     ErrorCode = "UNAUTHORIZED"
	FORBIDDEN        ErrorCode = "FORBIDDEN"
	RATE_LIMITED     ErrorCode = "RATE_LIMITED"
	NOT_FOUND_ERROR  ErrorCode = "NOT_FOUND_ERROR"
	VALIDATION_ERROR ErrorCode = "VALIDATION_ERROR"

	// Package scanning errors
	PACKAGE_NOT_FOUND ErrorCode = "PACKAGE_NOT_FOUND"
	SCAN_FAILED       ErrorCode = "SCAN_FAILED"
	INVALID_PACKAGE   ErrorCode = "INVALID_PACKAGE"

	// Database errors
	DB_CONNECTION_ERROR  ErrorCode = "DB_CONNECTION_ERROR"
	DB_QUERY_ERROR       ErrorCode = "DB_QUERY_ERROR"
	DB_TRANSACTION_ERROR ErrorCode = "DB_TRANSACTION_ERROR"

	// Cache errors
	CACHE_ERROR ErrorCode = "CACHE_ERROR"
	CACHE_MISS  ErrorCode = "CACHE_MISS"

	// ML/AI errors
	ML_MODEL_ERROR      ErrorCode = "ML_MODEL_ERROR"
	ML_PREDICTION_ERROR ErrorCode = "ML_PREDICTION_ERROR"

	// Configuration errors
	CONFIG_ERROR            ErrorCode = "CONFIG_ERROR"
	CONFIG_VALIDATION_ERROR ErrorCode = "CONFIG_VALIDATION_ERROR"

	// Input validation errors
	ErrCodeValidation      ErrorCode = "VALIDATION_ERROR"
	ErrCodeInvalidInput    ErrorCode = "INVALID_INPUT"
	ErrCodeMissingRequired ErrorCode = "MISSING_REQUIRED"

	// Network and external service errors
	ErrCodeNetwork            ErrorCode = "NETWORK_ERROR"
	ErrCodeTimeout            ErrorCode = "TIMEOUT_ERROR"
	ErrCodeRateLimit          ErrorCode = "RATE_LIMIT_ERROR"
	ErrCodeServiceUnavailable ErrorCode = "SERVICE_UNAVAILABLE"

	// Resource errors
	ErrCodeNotFound         ErrorCode = "NOT_FOUND"
	ErrCodeAlreadyExists    ErrorCode = "ALREADY_EXISTS"
	ErrCodePermissionDenied ErrorCode = "PERMISSION_DENIED"
	ErrCodeQuotaExceeded    ErrorCode = "QUOTA_EXCEEDED"

	// Processing errors
	ErrCodeProcessing ErrorCode = "PROCESSING_ERROR"
	ErrCodeParsing    ErrorCode = "PARSING_ERROR"
	ErrCodeEncoding   ErrorCode = "ENCODING_ERROR"
	ErrCodeDecoding   ErrorCode = "DECODING_ERROR"

	// Database errors
	ErrCodeDatabase    ErrorCode = "DATABASE_ERROR"
	ErrCodeTransaction ErrorCode = "TRANSACTION_ERROR"
	ErrCodeConstraint  ErrorCode = "CONSTRAINT_ERROR"

	// Authentication and authorization errors
	ErrCodeAuth         ErrorCode = "AUTH_ERROR"
	ErrCodeUnauthorized ErrorCode = "UNAUTHORIZED"
	ErrCodeForbidden    ErrorCode = "FORBIDDEN"
	ErrCodeTokenExpired ErrorCode = "TOKEN_EXPIRED"

	// Configuration errors
	ErrCodeConfig        ErrorCode = "CONFIG_ERROR"
	ErrCodeMissingConfig ErrorCode = "MISSING_CONFIG"
	ErrCodeInvalidConfig ErrorCode = "INVALID_CONFIG"

	// Internal system errors
	ErrCodeInternal ErrorCode = "INTERNAL_ERROR"
	ErrCodePanic    ErrorCode = "PANIC_ERROR"
	ErrCodeUnknown  ErrorCode = "UNKNOWN_ERROR"
)

type ErrorList

type ErrorList struct {
	Errors []*AppError `json:"errors"`
}

ErrorList represents a collection of errors

func NewErrorList

func NewErrorList() *ErrorList

NewErrorList creates a new error list

func (*ErrorList) Add

func (el *ErrorList) Add(err *AppError)

Add adds an error to the list

func (*ErrorList) AddError

func (el *ErrorList) AddError(err error)

AddError adds a generic error to the list

func (*ErrorList) Clear

func (el *ErrorList) Clear()

Clear removes all errors from the list

func (*ErrorList) Count

func (el *ErrorList) Count() int

Count returns the number of errors

func (*ErrorList) Error

func (el *ErrorList) Error() string

Error implements the error interface

func (*ErrorList) First

func (el *ErrorList) First() *AppError

First returns the first error in the list

func (*ErrorList) HasErrors

func (el *ErrorList) HasErrors() bool

HasErrors returns true if the list contains errors

type Severity

type Severity string

Severity represents the severity level of an error

const (
	SeverityLow      Severity = "LOW"
	SeverityMedium   Severity = "MEDIUM"
	SeverityHigh     Severity = "HIGH"
	SeverityCritical Severity = "CRITICAL"
)

func GetSeverity

func GetSeverity(err error) Severity

GetSeverity returns the severity of an error

Jump to

Keyboard shortcuts

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