Documentation
¶
Overview ¶
Package errors provides structured error handling with error codes, categories, and HTTP response helpers for the beamdrop application.
Index ¶
- func GetHTTPStatus(err error) int
- func HasCode(err error, code Code) bool
- func IsAuth(err error) bool
- func IsConflict(err error) bool
- func IsNotFound(err error) bool
- func IsRetryable(err error) bool
- func IsStorage(err error) bool
- func IsValidation(err error) bool
- func SendError(w http.ResponseWriter, err error)
- func SendErrorWithRequestID(w http.ResponseWriter, err error, requestID string)
- type APIErrorBody
- type APIErrorResponse
- type Category
- type Code
- type Error
- func APIKeyExpired() *Error
- func BandwidthExceeded() *Error
- func BucketExists(bucket string) *Error
- func BucketNotEmpty(bucket string) *Error
- func BucketNotFound(bucket string) *Error
- func ChecksumMismatch() *Error
- func DatabaseError(message string) *Error
- func FileExists(path string) *Error
- func FileNotFound(path string) *Error
- func FileTooLarge(maxSize string) *Error
- func Forbidden(message string) *Error
- func FromError(err error) *Error
- func IOError(message string) *Error
- func InternalError(message string) *Error
- func InvalidAPIKey() *Error
- func InvalidBucketName(message string) *Error
- func InvalidMIMEType(mimeType string) *Error
- func InvalidObjectKey(message string) *Error
- func InvalidPassword() *Error
- func InvalidPath(message string) *Error
- func InvalidRequest(message string) *Error
- func InvalidToken(message string) *Error
- func LinkNotFound(id string) *Error
- func MaintenanceMode(retryAfter time.Duration) *Error
- func MissingField(field string) *Error
- func New(code Code, category Category, message string, httpStatus int) *Error
- func ObjectExists(key string) *Error
- func ObjectLocked(key string) *Error
- func ObjectNotFound(key string) *Error
- func PathNotFound(path string) *Error
- func PermissionDenied(resource string) *Error
- func QuotaExceeded(message string) *Error
- func RateLimitExceeded(retryAfter time.Duration) *Error
- func ReadFailed(message string) *Error
- func ServiceUnavailable(retryAfter time.Duration) *Error
- func StorageFull() *Error
- func TokenExpired() *Error
- func TooManyRequests(retryAfter time.Duration) *Error
- func Unauthorized(message string) *Error
- func Wrap(err error, code Code, category Category, message string, httpStatus int) *Error
- func WriteFailed(message string) *Error
- func (e *Error) Error() string
- func (e *Error) Is(target error) bool
- func (e *Error) Unwrap() error
- func (e *Error) WithCause(cause error) *Error
- func (e *Error) WithDetail(key string, value any) *Error
- func (e *Error) WithDetails(details map[string]any) *Error
- func (e *Error) WithRetryAfter(d time.Duration) *Error
- func (e *Error) WriteHTTPResponse(w http.ResponseWriter)
- func (e *Error) WriteHTTPResponseWithRequestID(w http.ResponseWriter, requestID string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetHTTPStatus ¶
GetHTTPStatus returns the HTTP status code for an error
func IsConflict ¶
IsConflict returns true if the error is a conflict error
func IsNotFound ¶
IsNotFound returns true if the error is a not-found error
func IsRetryable ¶
IsRetryable returns true if the error indicates the operation can be retried
func IsValidation ¶
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 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 ( 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 ( CodeMaintenanceMode Code = "MAINTENANCE_MODE" )
Unavailable error codes
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 BucketNotEmpty ¶
func BucketNotFound ¶
func ChecksumMismatch ¶
func ChecksumMismatch() *Error
func DatabaseError ¶
func FileExists ¶
func FileNotFound ¶
func FileTooLarge ¶
func FromError ¶
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 InternalError ¶
func InvalidAPIKey ¶
func InvalidAPIKey() *Error
func InvalidBucketName ¶
func InvalidMIMEType ¶
func InvalidObjectKey ¶
func InvalidPassword ¶
func InvalidPassword() *Error
func InvalidPath ¶
func InvalidRequest ¶
func InvalidToken ¶
func LinkNotFound ¶
func MaintenanceMode ¶
func MissingField ¶
func ObjectExists ¶
func ObjectLocked ¶
func ObjectNotFound ¶
func PathNotFound ¶
func PermissionDenied ¶
func QuotaExceeded ¶
func RateLimitExceeded ¶
func ReadFailed ¶
func ServiceUnavailable ¶
func StorageFull ¶
func StorageFull() *Error
func TokenExpired ¶
func TokenExpired() *Error
func TooManyRequests ¶
func Unauthorized ¶
func WriteFailed ¶
func (*Error) WithDetail ¶
WithDetail adds a single detail to the error
func (*Error) WithDetails ¶
WithDetails adds details to the error
func (*Error) WithRetryAfter ¶
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