github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/errors.go (about)

     1  package sqlx
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/errors"
     7  )
     8  
     9  var ErrNotTx = errors.New("db is not *sql.Tx")
    10  var ErrNotDB = errors.New("db is not *sql.DB")
    11  
    12  func NewSqlError(tpe sqlErrType, msg string) *SqlError {
    13  	return &SqlError{
    14  		Type: tpe,
    15  		Msg:  msg,
    16  	}
    17  }
    18  
    19  type SqlError struct {
    20  	Type sqlErrType
    21  	Msg  string
    22  }
    23  
    24  func (e *SqlError) Error() string {
    25  	return fmt.Sprintf("Sqlx [%s] %s", e.Type, e.Msg)
    26  }
    27  
    28  type sqlErrType string
    29  
    30  var (
    31  	SqlErrTypeNotFound sqlErrType = "NotFound"
    32  	SqlErrTypeConflict sqlErrType = "Conflict"
    33  )
    34  
    35  var DuplicateEntryErrNumber uint16 = 1062
    36  
    37  func DBErr(err error) *dbErr {
    38  	return &dbErr{
    39  		err: err,
    40  	}
    41  }
    42  
    43  type dbErr struct {
    44  	err error
    45  
    46  	errDefault  error
    47  	errNotFound error
    48  	errConflict error
    49  }
    50  
    51  func (r dbErr) WithNotFound(err error) *dbErr {
    52  	r.errNotFound = err
    53  	return &r
    54  }
    55  
    56  func (r dbErr) WithDefault(err error) *dbErr {
    57  	r.errDefault = err
    58  	return &r
    59  }
    60  
    61  func (r dbErr) WithConflict(err error) *dbErr {
    62  	r.errConflict = err
    63  	return &r
    64  }
    65  
    66  func (r *dbErr) IsNotFound() bool {
    67  	if sqlErr, ok := UnwrapAll(r.err).(*SqlError); ok {
    68  		return sqlErr.Type == SqlErrTypeNotFound
    69  	}
    70  	return false
    71  }
    72  
    73  func (r *dbErr) IsConflict() bool {
    74  	if sqlErr, ok := UnwrapAll(r.err).(*SqlError); ok {
    75  		return sqlErr.Type == SqlErrTypeConflict
    76  	}
    77  	return false
    78  }
    79  
    80  func (r *dbErr) Err() error {
    81  	if r.err == nil {
    82  		return nil
    83  	}
    84  	if e, ok := UnwrapAll(r.err).(*SqlError); ok {
    85  		switch e.Type {
    86  		case SqlErrTypeNotFound:
    87  			if r.errNotFound != nil {
    88  				return r.errNotFound
    89  			}
    90  		case SqlErrTypeConflict:
    91  			if r.errConflict != nil {
    92  				return r.errConflict
    93  			}
    94  		}
    95  		if r.errDefault != nil {
    96  			return r.errDefault
    97  		}
    98  	}
    99  	return r.err
   100  }
   101  
   102  func UnwrapAll(err error) error {
   103  	for {
   104  		if cause := UnwrapOnce(err); cause != nil {
   105  			err = cause
   106  			continue
   107  		}
   108  		break
   109  	}
   110  	return err
   111  }
   112  
   113  func UnwrapOnce(err error) (cause error) {
   114  	switch e := err.(type) {
   115  	case interface{ Cause() error }:
   116  		return e.Cause()
   117  	case interface{ Unwrap() error }:
   118  		return e.Unwrap()
   119  	}
   120  	return nil
   121  }
   122  
   123  // TODO should import some driver kit error list