errors

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package errors provides structured error handling with error codes, categories, and HTTP response helpers for the beamdrop 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

func HasCode

func HasCode(err error, code Code) bool

HasCode checks if an error has a specific error code

func IsAuth

func IsAuth(err error) bool

IsAuth returns true if the error is an authentication/authorization error

func IsConflict

func IsConflict(err error) bool

IsConflict returns true if the error is a conflict error

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the error is a not-found error

func IsRetryable

func IsRetryable(err error) bool

IsRetryable returns true if the error indicates the operation can be retried

func IsStorage

func IsStorage(err error) bool

IsStorage returns true if the error is a storage-related error

func IsValidation

func IsValidation(err error) bool

IsValidation returns true if the error is a validation error

func SendError

func SendError(w http.ResponseWriter, err error)

SendError is a convenience function to send any error as HTTP response It converts standard errors to structured errors if needed

func SendErrorWithRequestID

func SendErrorWithRequestID(w http.ResponseWriter, err error, requestID string)

SendErrorWithRequestID sends an error with request ID tracking

Types

type APIErrorBody

type APIErrorBody struct {
	Code     Code           `json:"code"`
	Message  string         `json:"message"`
	Category Category       `json:"category,omitempty"`
	Details  map[string]any `json:"details,omitempty"`
	// RequestID for tracing (optional, set by middleware)
	RequestID string `json:"requestId,omitempty"`
}

APIErrorBody represents the error body in API responses

type APIErrorResponse

type APIErrorResponse struct {
	Error APIErrorBody `json:"error"`
}

APIErrorResponse represents the JSON structure for API error responses

type Category

type Category string

Category represents the category of an error

const (
	// CategoryValidation for input validation errors
	CategoryValidation Category = "VALIDATION"
	// CategoryStorage for storage-related errors
	CategoryStorage Category = "STORAGE"
	// CategoryAuth for authentication/authorization errors
	CategoryAuth Category = "AUTH"
	// CategoryNotFound for resource not found errors
	CategoryNotFound Category = "NOT_FOUND"
	// CategoryConflict for resource conflict errors
	CategoryConflict Category = "CONFLICT"
	// CategoryRateLimit for rate limiting errors
	CategoryRateLimit Category = "RATE_LIMIT"
	// CategoryInternal for internal server errors
	CategoryInternal Category = "INTERNAL"
	// CategoryUnavailable for service unavailable errors
	CategoryUnavailable Category = "UNAVAILABLE"
)

type Code

type Code string

Code represents a specific error code

const (
	CodeInvalidRequest    Code = "INVALID_REQUEST"
	CodeInvalidBucketName Code = "INVALID_BUCKET_NAME"
	CodeInvalidObjectKey  Code = "INVALID_OBJECT_KEY"
	CodeInvalidPath       Code = "INVALID_PATH"
	CodeInvalidMIMEType   Code = "INVALID_MIME_TYPE"
	CodeFileTooLarge      Code = "FILE_TOO_LARGE"
	CodeMissingField      Code = "MISSING_FIELD"
)

Validation error codes

const (
	CodeStorageFull      Code = "STORAGE_FULL"
	CodeQuotaExceeded    Code = "QUOTA_EXCEEDED"
	CodeObjectLocked     Code = "OBJECT_LOCKED"
	CodeWriteFailed      Code = "WRITE_FAILED"
	CodeReadFailed       Code = "READ_FAILED"
	CodeDeleteFailed     Code = "DELETE_FAILED"
	CodeIOError          Code = "IO_ERROR"
	CodeChecksumMismatch Code = "CHECKSUM_MISMATCH"
)

Storage error codes

const (
	CodeUnauthorized     Code = "UNAUTHORIZED"
	CodeForbidden        Code = "FORBIDDEN"
	CodeInvalidToken     Code = "INVALID_TOKEN"
	CodeTokenExpired     Code = "TOKEN_EXPIRED"
	CodeInvalidAPIKey    Code = "INVALID_API_KEY"
	CodeAPIKeyExpired    Code = "API_KEY_EXPIRED"
	CodeInvalidPassword  Code = "INVALID_PASSWORD"
	CodeSessionExpired   Code = "SESSION_EXPIRED"
	CodePermissionDenied Code = "PERMISSION_DENIED"
)

Auth error codes

const (
	CodeBucketNotFound Code = "BUCKET_NOT_FOUND"
	CodeObjectNotFound Code = "OBJECT_NOT_FOUND"
	CodeFileNotFound   Code = "FILE_NOT_FOUND"
	CodePathNotFound   Code = "PATH_NOT_FOUND"
	CodeLinkNotFound   Code = "LINK_NOT_FOUND"
)

Not found error codes

const (
	CodeBucketExists    Code = "BUCKET_EXISTS"
	CodeObjectExists    Code = "OBJECT_EXISTS"
	CodeFileExists      Code = "FILE_EXISTS"
	CodeBucketNotEmpty  Code = "BUCKET_NOT_EMPTY"
	CodeVersionConflict Code = "VERSION_CONFLICT"
)

Conflict error codes

const (
	CodeRateLimitExceeded Code = "RATE_LIMIT_EXCEEDED"
	CodeTooManyRequests   Code = "TOO_MANY_REQUESTS"
	CodeBandwidthExceeded Code = "BANDWIDTH_EXCEEDED"
)

Rate limit error codes

const (
	CodeInternalError   Code = "INTERNAL_ERROR"
	CodeDatabaseError   Code = "DATABASE_ERROR"
	CodeConfigError     Code = "CONFIG_ERROR"
	CodeUnexpectedError Code = "UNEXPECTED_ERROR"
)

Internal error codes

const (
	CodeServiceUnavailable     Code = "SERVICE_UNAVAILABLE"
	CodeMaintenanceMode        Code = "MAINTENANCE_MODE"
	CodeTemporarilyUnavailable Code = "TEMPORARILY_UNAVAILABLE"
)

Unavailable error codes

func GetCode

func GetCode(err error) Code

GetCode returns the error code if available

type Error

type Error struct {
	// Code is the machine-readable error code
	Code Code `json:"code"`
	// Category is the error category
	Category Category `json:"category"`
	// Message is the human-readable error message
	Message string `json:"message"`
	// Details contains additional error details (optional)
	Details map[string]any `json:"details,omitempty"`
	// HTTPStatus is the HTTP status code to return
	HTTPStatus int `json:"-"`
	// Retryable indicates if the operation can be retried
	Retryable bool `json:"-"`
	// RetryAfter indicates when the operation can be retried (for rate limiting)
	RetryAfter time.Duration `json:"-"`
	// Cause is the underlying error (for wrapping)
	Cause error `json:"-"`
}

Error represents a structured application error

func APIKeyExpired

func APIKeyExpired() *Error

func BandwidthExceeded

func BandwidthExceeded() *Error

func BucketExists

func BucketExists(bucket string) *Error

func BucketNotEmpty

func BucketNotEmpty(bucket string) *Error

func BucketNotFound

func BucketNotFound(bucket string) *Error

func ChecksumMismatch

func ChecksumMismatch() *Error

func DatabaseError

func DatabaseError(message string) *Error

func FileExists

func FileExists(path string) *Error

func FileNotFound

func FileNotFound(path string) *Error

func FileTooLarge

func FileTooLarge(maxSize string) *Error

func Forbidden

func Forbidden(message string) *Error

func FromError

func FromError(err error) *Error

FromError attempts to convert a standard error to a structured Error If the error is already a structured Error, it returns it as-is Otherwise, it wraps it as an internal error

func IOError

func IOError(message string) *Error

func InternalError

func InternalError(message string) *Error

func InvalidAPIKey

func InvalidAPIKey() *Error

func InvalidBucketName

func InvalidBucketName(message string) *Error

func InvalidMIMEType

func InvalidMIMEType(mimeType string) *Error

func InvalidObjectKey

func InvalidObjectKey(message string) *Error

func InvalidPassword

func InvalidPassword() *Error

func InvalidPath

func InvalidPath(message string) *Error

func InvalidRequest

func InvalidRequest(message string) *Error

func InvalidToken

func InvalidToken(message string) *Error

func LinkNotFound

func LinkNotFound(id string) *Error

func MaintenanceMode

func MaintenanceMode(retryAfter time.Duration) *Error

func MissingField

func MissingField(field string) *Error

func New

func New(code Code, category Category, message string, httpStatus int) *Error

New creates a new Error with the given parameters

func ObjectExists

func ObjectExists(key string) *Error

func ObjectLocked

func ObjectLocked(key string) *Error

func ObjectNotFound

func ObjectNotFound(key string) *Error

func PathNotFound

func PathNotFound(path string) *Error

func PermissionDenied

func PermissionDenied(resource string) *Error

func QuotaExceeded

func QuotaExceeded(message string) *Error

func RateLimitExceeded

func RateLimitExceeded(retryAfter time.Duration) *Error

func ReadFailed

func ReadFailed(message string) *Error

func ServiceUnavailable

func ServiceUnavailable(retryAfter time.Duration) *Error

func StorageFull

func StorageFull() *Error

func TokenExpired

func TokenExpired() *Error

func TooManyRequests

func TooManyRequests(retryAfter time.Duration) *Error

func Unauthorized

func Unauthorized(message string) *Error

func Wrap

func Wrap(err error, code Code, category Category, message string, httpStatus int) *Error

Wrap wraps an existing error with a structured Error

func WriteFailed

func WriteFailed(message string) *Error

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface

func (*Error) Is

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

Is implements error comparison

func (*Error) Unwrap

func (e *Error) Unwrap() error

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

func (*Error) WithCause

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

WithCause wraps an underlying error

func (*Error) WithDetail

func (e *Error) WithDetail(key string, value any) *Error

WithDetail adds a single detail to the error

func (*Error) WithDetails

func (e *Error) WithDetails(details map[string]any) *Error

WithDetails adds details to the error

func (*Error) WithRetryAfter

func (e *Error) WithRetryAfter(d time.Duration) *Error

WithRetryAfter sets the retry-after duration

func (*Error) WriteHTTPResponse

func (e *Error) WriteHTTPResponse(w http.ResponseWriter)

WriteHTTPResponse writes the error as an HTTP JSON response

func (*Error) WriteHTTPResponseWithRequestID

func (e *Error) WriteHTTPResponseWithRequestID(w http.ResponseWriter, requestID string)

WriteHTTPResponseWithRequestID writes the error with a request ID

Jump to

Keyboard shortcuts

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