diff --git a/errors/error.go b/errors/error.go index 9253cda0083d017bfe7872010dcfc5aec9f893f7..658e36eba679fc8f307da59067201e9f8bf9643d 100644 --- a/errors/error.go +++ b/errors/error.go @@ -118,20 +118,23 @@ func (err CustomError) Formatted(opts FormattingOptions) string { err.caller.Function(), ) } - thisError += err.message - if !opts.Causes { - return thisError + + if err.cause == nil || !opts.Causes { + return err.message } - if err.cause == nil { - return thisError + if err.cause.Error() != err.message { + thisError += err.message } - sep := ", because" - if opts.NewLines { - sep += "\n\t" - } else { - sep += " " + sep := "" + if len(thisError) > 0 { + sep = ", because" + if opts.NewLines { + sep += "\n\t" + } else { + sep += " " + } } if causeWithStack, ok := err.cause.(*CustomError); ok { diff --git a/errors/errors.go b/errors/errors.go index 8feac4f9d5e47eab499cfa2545bee3f8d4eb0aaa..9c6ec15ab34b0d7a1a118a1bbce4239b84aa82ac 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -88,6 +88,30 @@ func HTTP(code int, err error, format string, args ...interface{}) error { return wrappedErr } +func HTTPWithMsg(code int, format string, args ...interface{}) error { + errorString := fmt.Sprintf(format, args...) + + wrappedErr := &CustomError{ + code: code, + message: errorString, + caller: GetCaller(2), + cause: Error(errorString), + } + + return wrappedErr +} + +func HTTPWithError(code int, err error) error { + wrappedErr := &CustomError{ + code: code, + message: err.Error(), + caller: GetCaller(2), + cause: err, + } + + return wrappedErr +} + type Description struct { Message string `json:"error"` Source *CallerInfo `json:"source,omitempty"` diff --git a/errors/http_error_test.go b/errors/http_error_test.go index 609d47a7666c85b974803ed4cdfadd262f511ea1..97d7ae35502c545263db621113a6d168df81a3eb 100644 --- a/errors/http_error_test.go +++ b/errors/http_error_test.go @@ -17,9 +17,6 @@ func TestHTTPError(t *testing.T) { //or if you know you are creating the HTTP error, do it as one statement err = errors.HTTP(http.StatusBadRequest, err, "failed to get user") - //and one more to give a lower code again - err = errors.HTTP(http.StatusInsufficientStorage, err, "jissis this is bad!") - //and higher again -... many layers... err = errors.HTTP(http.StatusNotFound, err, "terrible mistake") @@ -31,9 +28,6 @@ func TestHTTPError(t *testing.T) { err = errors.Errorf("failed to connect to db") //err = errors.HTTP(http.StatusInternalServerError, err) - //and one more to give a lower code again - err = errors.HTTP(http.StatusInsufficientStorage, err, "jissis this is bad!") - //and higher again -... many layers... err = errors.HTTP(http.StatusNotFound, err, "terrible mistake")