github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/dberr/err.go (about) 1 package dberr 2 3 import "fmt" 4 5 func New(tpe errType, msg string) *SqlError { 6 return &SqlError{ 7 Type: tpe, 8 Msg: msg, 9 } 10 } 11 12 type SqlError struct { 13 Type errType 14 Msg string 15 } 16 17 func (e *SqlError) Error() string { 18 return fmt.Sprintf("Sqlx [%s] %s", e.Type, e.Msg) 19 } 20 21 type errType string 22 23 var ( 24 ErrTypeNotFound errType = "NotFound" 25 ErrTypeConflict errType = "Conflict" 26 ErrTypeRolledBack errType = "RolledBack" 27 ) 28 29 func IsErrNotFound(err error) bool { 30 if err == nil { 31 return false 32 } 33 if sqlErr, ok := UnwrapAll(err).(*SqlError); ok { 34 return sqlErr.Type == ErrTypeNotFound 35 } 36 return false 37 } 38 39 func IsErrConflict(err error) bool { 40 if err == nil { 41 return false 42 } 43 if sqlErr, ok := UnwrapAll(err).(*SqlError); ok { 44 return sqlErr.Type == ErrTypeConflict 45 } 46 return false 47 } 48 49 func IsErrRolledBack(err error) bool { 50 if err == nil { 51 return false 52 } 53 if sqlErr, ok := UnwrapAll(err).(*SqlError); ok { 54 return sqlErr.Type == ErrTypeRolledBack 55 } 56 return false 57 }