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 ¶
- func GetHTTPStatus(err error) int
- func Is(err error, code Code) bool
- func WriteHTTPError(w http.ResponseWriter, err *AppError)
- type AppError
- func AuthError(code Code, message string) *AppError
- func CacheError(code Code, message string, cause error) *AppError
- func ConfigError(message string, cause error) *AppError
- func FromHTTPStatus(status int, message string) *AppError
- func InternalError(message string, cause error) *AppError
- func MirrorError(code Code, message string, cause error) *AppError
- func New(code Code, message string) *AppError
- func ServerError(code Code, message string, cause error) *AppError
- func Wrap(code Code, message string, cause error) *AppError
- type Code
- type HTTPErrorResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetHTTPStatus ¶
GetHTTPStatus returns the HTTP status code for an error. If err is not an AppError, returns 500 Internal Server Error.
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 CacheError ¶
CacheError creates a cache-related error.
func ConfigError ¶
ConfigError creates a configuration error.
func FromHTTPStatus ¶
FromHTTPStatus creates an AppError from an HTTP status code.
func InternalError ¶
InternalError creates an internal server error.
func MirrorError ¶
MirrorError creates a mirror-related error.
func ServerError ¶
ServerError creates a server-related error.
func (*AppError) WithCause ¶
WithCause sets the underlying cause of this error and returns the error.
func (*AppError) WithDetails ¶
WithDetails adds additional details to the error and returns the error.
func (*AppError) WithHTTPStatus ¶
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.