github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/dberr/errors.go (about)

     1  package dberr
     2  
     3  import (
     4  	"fmt"
     5  
     6  	kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
     7  )
     8  
     9  const (
    10  	CodeInternal      = 1
    11  	CodeNotFound      = 2
    12  	CodeAlreadyExists = 3
    13  	CodeConflict      = 4
    14  )
    15  
    16  type DBErrReason = kebError.ErrReason
    17  
    18  const (
    19  	ErrDBInternal      DBErrReason = "err_db_internal"
    20  	ErrDBNotFound      DBErrReason = "err_db_not_found"
    21  	ErrDBAlreadyExists DBErrReason = "err_db_already_exists"
    22  	ErrDBConflict      DBErrReason = "err_db_conflict"
    23  	ErrDBUnknown       DBErrReason = "err_db_unknown"
    24  )
    25  
    26  type Error interface {
    27  	Append(string, ...interface{}) Error
    28  	Code() int
    29  	Error() string
    30  }
    31  
    32  type dbError struct {
    33  	code    int
    34  	message string
    35  }
    36  
    37  func errorf(code int, format string, a ...interface{}) Error {
    38  	return dbError{code: code, message: fmt.Sprintf(format, a...)}
    39  }
    40  
    41  func Internal(format string, a ...interface{}) Error {
    42  	return errorf(CodeInternal, format, a...)
    43  }
    44  
    45  func NotFound(format string, a ...interface{}) Error {
    46  	return errorf(CodeNotFound, format, a...)
    47  }
    48  
    49  func IsNotFound(err error) bool {
    50  	nf, ok := err.(interface {
    51  		Code() int
    52  	})
    53  	return ok && nf.Code() == CodeNotFound
    54  }
    55  
    56  func AlreadyExists(format string, a ...interface{}) Error {
    57  	return errorf(CodeAlreadyExists, format, a...)
    58  }
    59  
    60  func Conflict(format string, a ...interface{}) Error {
    61  	return errorf(CodeConflict, format, a...)
    62  }
    63  
    64  func (e dbError) Append(additionalFormat string, a ...interface{}) Error {
    65  	format := additionalFormat + ", " + e.message
    66  	return errorf(e.code, format, a...)
    67  }
    68  
    69  func (e dbError) Code() int {
    70  	return e.code
    71  }
    72  
    73  func (e dbError) Error() string {
    74  	return e.message
    75  }
    76  
    77  func (e dbError) Component() kebError.ErrComponent {
    78  	return kebError.ErrDB
    79  }
    80  
    81  func (e dbError) Reason() DBErrReason {
    82  	reason := ErrDBUnknown
    83  
    84  	switch e.code {
    85  	case CodeInternal:
    86  		reason = ErrDBInternal
    87  	case CodeNotFound:
    88  		reason = ErrDBNotFound
    89  	case CodeAlreadyExists:
    90  		reason = ErrDBAlreadyExists
    91  	case CodeConflict:
    92  		reason = ErrDBConflict
    93  	}
    94  
    95  	return reason
    96  }
    97  
    98  func IsConflict(err error) bool {
    99  	dbe, ok := err.(Error)
   100  	if !ok {
   101  		return false
   102  	}
   103  	return dbe.Code() == CodeConflict
   104  }