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

     1  package edp
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
     8  )
     9  
    10  type edpError struct {
    11  	id      string
    12  	code    int
    13  	message string
    14  }
    15  
    16  type EDPErrReason = kebError.ErrReason
    17  
    18  const (
    19  	ErrEDPConflict   EDPErrReason = "err_edp_internal"
    20  	ErrEDPNotFound   EDPErrReason = "err_edp_not_found"
    21  	ErrEDPBadRequest EDPErrReason = "err_edp_bad_request"
    22  	ErrEDPTimeout    EDPErrReason = "err_edp_timeout"
    23  	ErrEDPOther      EDPErrReason = "err_edp_other"
    24  )
    25  
    26  func errorf(id string, code int, format string, args ...interface{}) kebError.ErrorReporter {
    27  	return edpError{id: id, code: code, message: fmt.Sprintf(format, args...)}
    28  }
    29  
    30  func NewEDPConflictError(id string, format string, args ...interface{}) kebError.ErrorReporter {
    31  	return errorf(id, http.StatusConflict, format, args...)
    32  }
    33  
    34  func NewEDPNotFoundError(id string, format string, args ...interface{}) kebError.ErrorReporter {
    35  	return errorf(id, http.StatusNotFound, format, args...)
    36  }
    37  
    38  func NewEDPBadRequestError(id string, format string, args ...interface{}) kebError.ErrorReporter {
    39  	return errorf(id, http.StatusBadRequest, format, args...)
    40  }
    41  
    42  func NewEDPOtherError(id string, code int, format string, args ...interface{}) kebError.ErrorReporter {
    43  	return errorf(id, code, format, args...)
    44  }
    45  
    46  func (e edpError) Error() string {
    47  	return e.message
    48  }
    49  
    50  func (e edpError) Code() int {
    51  	return e.code
    52  }
    53  
    54  func (e edpError) Component() kebError.ErrComponent {
    55  	return kebError.ErrEDP
    56  }
    57  
    58  func (e edpError) Reason() EDPErrReason {
    59  	reason := ErrEDPOther
    60  
    61  	switch e.code {
    62  	case http.StatusConflict:
    63  		reason = ErrEDPConflict
    64  	case http.StatusNotFound:
    65  		reason = ErrEDPNotFound
    66  	case http.StatusBadRequest:
    67  		reason = ErrEDPBadRequest
    68  	case http.StatusRequestTimeout:
    69  		reason = ErrEDPTimeout
    70  	}
    71  
    72  	return reason
    73  }
    74  
    75  func IsConflictError(err error) bool {
    76  	e, ok := err.(edpError)
    77  	if !ok {
    78  		return false
    79  	}
    80  	return e.Code() == http.StatusConflict
    81  }