From d4e877ef3d2ea95cdb13054eee7320bcdc6934ae Mon Sep 17 00:00:00 2001
From: Cornelius Rautenbach <cornel.rautenbach@gmail.com>
Date: Fri, 10 Dec 2021 13:39:38 +0200
Subject: [PATCH] HTTP and error changes

---
 errors/error.go           | 23 +++++++++++++----------
 errors/errors.go          | 24 ++++++++++++++++++++++++
 errors/http_error_test.go |  6 ------
 3 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/errors/error.go b/errors/error.go
index 9253cda..658e36e 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 8feac4f..9c6ec15 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 609d47a..97d7ae3 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")
 
-- 
GitLab