123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package errors
- import (
- stderrors "errors"
- "fmt"
- )
- // Is reports whether any error in err's chain matches target.
- var Is = stderrors.Is
- // As finds the first error in err's chain that matches target, and if so, sets
- // target to that error value and returns true.
- var As = stderrors.As
- // Unwrap returns the result of calling the Unwrap method on err, if err's
- // type contains an Unwrap method returning error.
- // Otherwise, Unwrap returns nil.
- var Unwrap = stderrors.Unwrap
- // Standard errors that can be used across packages
- var (
- ErrNotFound = stderrors.New("resource not found")
- ErrInvalidInput = stderrors.New("invalid input")
- ErrUnauthorized = stderrors.New("unauthorized")
- ErrInternalServer = stderrors.New("internal server error")
- ErrDatabaseOperation = stderrors.New("database operation failed")
- )
- // DatabaseError represents a database-related error
- type DatabaseError struct {
- Operation string
- Err error
- }
- func (e *DatabaseError) Error() string {
- return fmt.Sprintf("database error during %s: %v", e.Operation, e.Err)
- }
- func (e *DatabaseError) Unwrap() error {
- return e.Err
- }
- // NewDatabaseError creates a new DatabaseError
- func NewDatabaseError(operation string, err error) *DatabaseError {
- return &DatabaseError{
- Operation: operation,
- Err: err,
- }
- }
- // AuthError represents an authentication-related error
- type AuthError struct {
- Action string
- Err error
- }
- func (e *AuthError) Error() string {
- return fmt.Sprintf("authentication error during %s: %v", e.Action, e.Err)
- }
- func (e *AuthError) Unwrap() error {
- return e.Err
- }
- // NewAuthError creates a new AuthError
- func NewAuthError(action string, err error) *AuthError {
- return &AuthError{
- Action: action,
- Err: err,
- }
- }
- // StorageError represents a storage-related error
- type StorageError struct {
- Operation string
- Bucket string
- Err error
- }
- func (e *StorageError) Error() string {
- return fmt.Sprintf("storage error during %s on bucket %s: %v", e.Operation, e.Bucket, e.Err)
- }
- func (e *StorageError) Unwrap() error {
- return e.Err
- }
- // NewStorageError creates a new StorageError
- func NewStorageError(operation, bucket string, err error) *StorageError {
- return &StorageError{
- Operation: operation,
- Bucket: bucket,
- Err: err,
- }
- }
- // ConfigError represents a configuration-related error
- type ConfigError struct {
- Section string
- Err error
- }
- func (e *ConfigError) Error() string {
- return fmt.Sprintf("configuration error in %s: %v", e.Section, e.Err)
- }
- func (e *ConfigError) Unwrap() error {
- return e.Err
- }
- // NewConfigError creates a new ConfigError
- func NewConfigError(section string, err error) *ConfigError {
- return &ConfigError{
- Section: section,
- Err: err,
- }
- }
- // SMTPError represents an email-related error
- type SMTPError struct {
- Operation string
- Err error
- }
- func (e *SMTPError) Error() string {
- return fmt.Sprintf("smtp error during %s: %v", e.Operation, e.Err)
- }
- func (e *SMTPError) Unwrap() error {
- return e.Err
- }
- // NewSMTPError creates a new SMTPError
- func NewSMTPError(operation string, err error) *SMTPError {
- return &SMTPError{
- Operation: operation,
- Err: err,
- }
- }
|