errors.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Package storage provides object storage functionality
  2. package storage
  3. import (
  4. "errors"
  5. "fmt"
  6. )
  7. // Common storage error types
  8. var (
  9. // ErrInvalidConfig indicates missing or invalid storage configuration
  10. ErrInvalidConfig = errors.New("invalid storage configuration")
  11. // ErrClientInitFailed indicates failure to initialize storage client
  12. ErrClientInitFailed = errors.New("failed to initialize storage client")
  13. // ErrBucketNotFound indicates the specified bucket does not exist
  14. ErrBucketNotFound = errors.New("bucket not found")
  15. // ErrBucketCreationFailed indicates failure to create a new bucket
  16. ErrBucketCreationFailed = errors.New("failed to create bucket")
  17. // ErrObjectNotFound indicates the requested object does not exist
  18. ErrObjectNotFound = errors.New("object not found")
  19. // ErrUploadFailed indicates failure to upload an object
  20. ErrUploadFailed = errors.New("failed to upload object")
  21. // ErrDownloadFailed indicates failure to download an object
  22. ErrDownloadFailed = errors.New("failed to download object")
  23. // ErrInvalidObjectName indicates an invalid object name was provided
  24. ErrInvalidObjectName = errors.New("invalid object name")
  25. // ErrInvalidObjectSize indicates an invalid object size was provided
  26. ErrInvalidObjectSize = errors.New("invalid object size")
  27. // ErrAuthenticationFailed indicates storage authentication failure
  28. ErrAuthenticationFailed = errors.New("storage authentication failed")
  29. )
  30. // Error represents a storage-specific error with detailed context
  31. type Error struct {
  32. // Op is the operation that failed (e.g., "upload", "download", "init")
  33. Op string
  34. // Bucket is the bucket involved in the operation (if applicable)
  35. Bucket string
  36. // Object is the object involved in the operation (if applicable)
  37. Object string
  38. // Err is the underlying error
  39. Err error
  40. // Details contains additional error context
  41. Details string
  42. }
  43. // Error returns a string representation of the error
  44. func (e *Error) Error() string {
  45. msg := fmt.Sprintf("storage %s failed", e.Op)
  46. if e.Bucket != "" {
  47. msg = fmt.Sprintf("%s for bucket %s", msg, e.Bucket)
  48. }
  49. if e.Object != "" {
  50. msg = fmt.Sprintf("%s at object %s", msg, e.Object)
  51. }
  52. if e.Details != "" {
  53. msg = fmt.Sprintf("%s: %s", msg, e.Details)
  54. }
  55. if e.Err != nil {
  56. msg = fmt.Sprintf("%s: %v", msg, e.Err)
  57. }
  58. return msg
  59. }
  60. // Unwrap returns the underlying error
  61. func (e *Error) Unwrap() error {
  62. return e.Err
  63. }
  64. // Is reports whether target matches this error
  65. func (e *Error) Is(target error) bool {
  66. return errors.Is(e.Err, target)
  67. }
  68. // NewError creates a new storage error
  69. func NewError(op string, err error, details string) *Error {
  70. return &Error{
  71. Op: op,
  72. Err: err,
  73. Details: details,
  74. }
  75. }
  76. // NewBucketError creates a new storage error with bucket context
  77. func NewBucketError(op string, bucket string, err error, details string) *Error {
  78. return &Error{
  79. Op: op,
  80. Bucket: bucket,
  81. Err: err,
  82. Details: details,
  83. }
  84. }
  85. // NewObjectError creates a new storage error with object context
  86. func NewObjectError(op string, bucket string, object string, err error, details string) *Error {
  87. return &Error{
  88. Op: op,
  89. Bucket: bucket,
  90. Object: object,
  91. Err: err,
  92. Details: details,
  93. }
  94. }
  95. // IsNotFoundError returns true if the error indicates a not found condition
  96. func IsNotFoundError(err error) bool {
  97. var e *Error
  98. return errors.As(err, &e) && (errors.Is(e.Err, ErrBucketNotFound) ||
  99. errors.Is(e.Err, ErrObjectNotFound))
  100. }
  101. // IsAuthError returns true if the error indicates an authentication problem
  102. func IsAuthError(err error) bool {
  103. var e *Error
  104. return errors.As(err, &e) && errors.Is(e.Err, ErrAuthenticationFailed)
  105. }