errors.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package errors
  2. import (
  3. stderrors "errors"
  4. "fmt"
  5. )
  6. // Is reports whether any error in err's chain matches target.
  7. var Is = stderrors.Is
  8. // As finds the first error in err's chain that matches target, and if so, sets
  9. // target to that error value and returns true.
  10. var As = stderrors.As
  11. // Unwrap returns the result of calling the Unwrap method on err, if err's
  12. // type contains an Unwrap method returning error.
  13. // Otherwise, Unwrap returns nil.
  14. var Unwrap = stderrors.Unwrap
  15. // Standard errors that can be used across packages
  16. var (
  17. ErrNotFound = stderrors.New("resource not found")
  18. ErrInvalidInput = stderrors.New("invalid input")
  19. ErrUnauthorized = stderrors.New("unauthorized")
  20. ErrInternalServer = stderrors.New("internal server error")
  21. ErrDatabaseOperation = stderrors.New("database operation failed")
  22. )
  23. // DatabaseError represents a database-related error
  24. type DatabaseError struct {
  25. Operation string
  26. Err error
  27. }
  28. func (e *DatabaseError) Error() string {
  29. return fmt.Sprintf("database error during %s: %v", e.Operation, e.Err)
  30. }
  31. func (e *DatabaseError) Unwrap() error {
  32. return e.Err
  33. }
  34. // NewDatabaseError creates a new DatabaseError
  35. func NewDatabaseError(operation string, err error) *DatabaseError {
  36. return &DatabaseError{
  37. Operation: operation,
  38. Err: err,
  39. }
  40. }
  41. // AuthError represents an authentication-related error
  42. type AuthError struct {
  43. Action string
  44. Err error
  45. }
  46. func (e *AuthError) Error() string {
  47. return fmt.Sprintf("authentication error during %s: %v", e.Action, e.Err)
  48. }
  49. func (e *AuthError) Unwrap() error {
  50. return e.Err
  51. }
  52. // NewAuthError creates a new AuthError
  53. func NewAuthError(action string, err error) *AuthError {
  54. return &AuthError{
  55. Action: action,
  56. Err: err,
  57. }
  58. }
  59. // StorageError represents a storage-related error
  60. type StorageError struct {
  61. Operation string
  62. Bucket string
  63. Err error
  64. }
  65. func (e *StorageError) Error() string {
  66. return fmt.Sprintf("storage error during %s on bucket %s: %v", e.Operation, e.Bucket, e.Err)
  67. }
  68. func (e *StorageError) Unwrap() error {
  69. return e.Err
  70. }
  71. // NewStorageError creates a new StorageError
  72. func NewStorageError(operation, bucket string, err error) *StorageError {
  73. return &StorageError{
  74. Operation: operation,
  75. Bucket: bucket,
  76. Err: err,
  77. }
  78. }
  79. // ConfigError represents a configuration-related error
  80. type ConfigError struct {
  81. Section string
  82. Err error
  83. }
  84. func (e *ConfigError) Error() string {
  85. return fmt.Sprintf("configuration error in %s: %v", e.Section, e.Err)
  86. }
  87. func (e *ConfigError) Unwrap() error {
  88. return e.Err
  89. }
  90. // NewConfigError creates a new ConfigError
  91. func NewConfigError(section string, err error) *ConfigError {
  92. return &ConfigError{
  93. Section: section,
  94. Err: err,
  95. }
  96. }
  97. // SMTPError represents an email-related error
  98. type SMTPError struct {
  99. Operation string
  100. Err error
  101. }
  102. func (e *SMTPError) Error() string {
  103. return fmt.Sprintf("smtp error during %s: %v", e.Operation, e.Err)
  104. }
  105. func (e *SMTPError) Unwrap() error {
  106. return e.Err
  107. }
  108. // NewSMTPError creates a new SMTPError
  109. func NewSMTPError(operation string, err error) *SMTPError {
  110. return &SMTPError{
  111. Operation: operation,
  112. Err: err,
  113. }
  114. }