errors

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package errors provides unified error handling for apt-proxy. It defines standard error codes, structured error types, and helper functions for consistent error management across the application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHTTPStatus

func GetHTTPStatus(err error) int

GetHTTPStatus returns the HTTP status code for an error. If err is not an AppError, returns 500 Internal Server Error.

func Is

func Is(err error, code Code) bool

Is checks if the error or any error in its unwrap chain is an AppError with the specified code. Uses standard library errors.Unwrap to walk the chain for compatibility with errors.As.

func WriteHTTPError

func WriteHTTPError(w http.ResponseWriter, err *AppError)

WriteHTTPError writes an AppError as an HTTP JSON response.

Types

type AppError

type AppError struct {
	// Code is the unique error code for this error type.
	Code Code `json:"code"`

	// Message is a human-readable error message.
	Message string `json:"message"`

	// Details contains optional additional error details.
	Details map[string]interface{} `json:"details,omitempty"`

	// HTTPStatus is the suggested HTTP status code for this error.
	HTTPStatus int `json:"-"`

	// Cause is the underlying error that caused this error.
	Cause error `json:"-"`
}

AppError represents a structured application error with code, message, and optional cause.

func AuthError

func AuthError(code Code, message string) *AppError

AuthError creates an authentication error.

func CacheError

func CacheError(code Code, message string, cause error) *AppError

CacheError creates a cache-related error.

func ConfigError

func ConfigError(message string, cause error) *AppError

ConfigError creates a configuration error.

func FromHTTPStatus

func FromHTTPStatus(status int, message string) *AppError

FromHTTPStatus creates an AppError from an HTTP status code.

func InternalError

func InternalError(message string, cause error) *AppError

InternalError creates an internal server error.

func MirrorError

func MirrorError(code Code, message string, cause error) *AppError

MirrorError creates a mirror-related error.

func New

func New(code Code, message string) *AppError

New creates a new AppError with the given code and message.

func ServerError

func ServerError(code Code, message string, cause error) *AppError

ServerError creates a server-related error.

func Wrap

func Wrap(code Code, message string, cause error) *AppError

Wrap wraps an existing error with a new AppError.

func (*AppError) Error

func (e *AppError) Error() string

Error implements the error interface.

func (*AppError) ToJSON

func (e *AppError) ToJSON() ([]byte, error)

ToJSON returns the error as a JSON byte slice.

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap returns the underlying error for errors.Is and errors.As support.

func (*AppError) WithCause

func (e *AppError) WithCause(cause error) *AppError

WithCause sets the underlying cause of this error and returns the error.

func (*AppError) WithDetails

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

WithDetails adds additional details to the error and returns the error.

func (*AppError) WithHTTPStatus

func (e *AppError) WithHTTPStatus(status int) *AppError

WithHTTPStatus sets the suggested HTTP status code and returns the error.

type Code

type Code string

Code represents a unique error code for categorizing errors.

const (
	// Configuration errors
	ErrConfigInvalid   Code = "CONFIG_INVALID"
	ErrConfigNotFound  Code = "CONFIG_NOT_FOUND"
	ErrConfigParseFail Code = "CONFIG_PARSE_FAILED"

	// Cache errors
	ErrCacheInit      Code = "CACHE_INIT_FAILED"
	ErrCacheRead      Code = "CACHE_READ_FAILED"
	ErrCacheWrite     Code = "CACHE_WRITE_FAILED"
	ErrCachePurge     Code = "CACHE_PURGE_FAILED"
	ErrCacheCleanup   Code = "CACHE_CLEANUP_FAILED"
	ErrCacheDirAccess Code = "CACHE_DIR_ACCESS_FAILED"

	// Mirror errors
	ErrMirrorTimeout     Code = "MIRROR_TIMEOUT"
	ErrMirrorUnreachable Code = "MIRROR_UNREACHABLE"
	ErrMirrorBenchmark   Code = "MIRROR_BENCHMARK_FAILED"
	ErrMirrorInvalid     Code = "MIRROR_INVALID"

	// Provider/upstream errors
	ErrProviderDown      Code = "PROVIDER_DOWN"
	ErrProviderTimeout   Code = "PROVIDER_TIMEOUT"
	ErrUpstreamError     Code = "UPSTREAM_ERROR"
	ErrUpstreamNotFound  Code = "UPSTREAM_NOT_FOUND"
	ErrUpstreamForbidden Code = "UPSTREAM_FORBIDDEN"

	// Server errors
	ErrServerInit     Code = "SERVER_INIT_FAILED"
	ErrServerShutdown Code = "SERVER_SHUTDOWN_FAILED"
	ErrServerStart    Code = "SERVER_START_FAILED"

	// Authentication errors
	ErrAuthRequired     Code = "AUTH_REQUIRED"
	ErrAuthInvalid      Code = "AUTH_INVALID"
	ErrAuthExpired      Code = "AUTH_EXPIRED"
	ErrAuthInsufficient Code = "AUTH_INSUFFICIENT"

	// Request errors
	ErrRequestInvalid   Code = "REQUEST_INVALID"
	ErrRequestTimeout   Code = "REQUEST_TIMEOUT"
	ErrMethodNotAllowed Code = "METHOD_NOT_ALLOWED"
	ErrResourceNotFound Code = "RESOURCE_NOT_FOUND"
	ErrRateLimited      Code = "RATE_LIMITED"

	// Internal errors
	ErrInternal       Code = "INTERNAL_ERROR"
	ErrUnknown        Code = "UNKNOWN_ERROR"
	ErrNotImplemented Code = "NOT_IMPLEMENTED"
)

Error codes for different error categories.

func GetCode

func GetCode(err error) Code

GetCode returns the error code if err is an AppError, otherwise returns ErrUnknown.

type HTTPErrorResponse

type HTTPErrorResponse struct {
	Error   string                 `json:"error"`
	Code    Code                   `json:"code,omitempty"`
	Message string                 `json:"message,omitempty"`
	Details map[string]interface{} `json:"details,omitempty"`
}

HTTPErrorResponse represents an error response in HTTP API.

Jump to

Keyboard shortcuts

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