123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- // Package storage provides object storage functionality
- package storage
- import (
- "errors"
- "fmt"
- )
- // Common storage error types
- var (
- // ErrInvalidConfig indicates missing or invalid storage configuration
- ErrInvalidConfig = errors.New("invalid storage configuration")
- // ErrClientInitFailed indicates failure to initialize storage client
- ErrClientInitFailed = errors.New("failed to initialize storage client")
- // ErrBucketNotFound indicates the specified bucket does not exist
- ErrBucketNotFound = errors.New("bucket not found")
- // ErrBucketCreationFailed indicates failure to create a new bucket
- ErrBucketCreationFailed = errors.New("failed to create bucket")
- // ErrObjectNotFound indicates the requested object does not exist
- ErrObjectNotFound = errors.New("object not found")
- // ErrUploadFailed indicates failure to upload an object
- ErrUploadFailed = errors.New("failed to upload object")
- // ErrDownloadFailed indicates failure to download an object
- ErrDownloadFailed = errors.New("failed to download object")
- // ErrInvalidObjectName indicates an invalid object name was provided
- ErrInvalidObjectName = errors.New("invalid object name")
- // ErrInvalidObjectSize indicates an invalid object size was provided
- ErrInvalidObjectSize = errors.New("invalid object size")
- // ErrAuthenticationFailed indicates storage authentication failure
- ErrAuthenticationFailed = errors.New("storage authentication failed")
- )
- // Error represents a storage-specific error with detailed context
- type Error struct {
- // Op is the operation that failed (e.g., "upload", "download", "init")
- Op string
- // Bucket is the bucket involved in the operation (if applicable)
- Bucket string
- // Object is the object involved in the operation (if applicable)
- Object string
- // Err is the underlying error
- Err error
- // Details contains additional error context
- Details string
- }
- // Error returns a string representation of the error
- func (e *Error) Error() string {
- msg := fmt.Sprintf("storage %s failed", e.Op)
- if e.Bucket != "" {
- msg = fmt.Sprintf("%s for bucket %s", msg, e.Bucket)
- }
- if e.Object != "" {
- msg = fmt.Sprintf("%s at object %s", msg, e.Object)
- }
- if e.Details != "" {
- msg = fmt.Sprintf("%s: %s", msg, e.Details)
- }
- if e.Err != nil {
- msg = fmt.Sprintf("%s: %v", msg, e.Err)
- }
- return msg
- }
- // Unwrap returns the underlying error
- func (e *Error) Unwrap() error {
- return e.Err
- }
- // Is reports whether target matches this error
- func (e *Error) Is(target error) bool {
- return errors.Is(e.Err, target)
- }
- // NewError creates a new storage error
- func NewError(op string, err error, details string) *Error {
- return &Error{
- Op: op,
- Err: err,
- Details: details,
- }
- }
- // NewBucketError creates a new storage error with bucket context
- func NewBucketError(op string, bucket string, err error, details string) *Error {
- return &Error{
- Op: op,
- Bucket: bucket,
- Err: err,
- Details: details,
- }
- }
- // NewObjectError creates a new storage error with object context
- func NewObjectError(op string, bucket string, object string, err error, details string) *Error {
- return &Error{
- Op: op,
- Bucket: bucket,
- Object: object,
- Err: err,
- Details: details,
- }
- }
- // IsNotFoundError returns true if the error indicates a not found condition
- func IsNotFoundError(err error) bool {
- var e *Error
- return errors.As(err, &e) && (errors.Is(e.Err, ErrBucketNotFound) ||
- errors.Is(e.Err, ErrObjectNotFound))
- }
- // IsAuthError returns true if the error indicates an authentication problem
- func IsAuthError(err error) bool {
- var e *Error
- return errors.As(err, &e) && errors.Is(e.Err, ErrAuthenticationFailed)
- }
|