Skip to content
Snippets Groups Projects
Commit d4e877ef authored by Cornel Rautenbach's avatar Cornel Rautenbach
Browse files

HTTP and error changes

parent d14e15cc
No related branches found
No related tags found
No related merge requests found
......@@ -118,21 +118,24 @@ 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"
sep := ""
if len(thisError) > 0 {
sep = ", because"
if opts.NewLines {
sep += "\n\t"
} else {
sep += " "
}
}
if causeWithStack, ok := err.cause.(*CustomError); ok {
return thisError + sep + causeWithStack.Formatted(opts)
......
......@@ -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"`
......
......@@ -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")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment