Skip to content

Commit

Permalink
Removed global formatter for error messages as it is potentially dang…
Browse files Browse the repository at this point in the history
…erous. One can unexpectedly change error messages from other packages if they also use simplerr. If error checks are using the error message then it could break things
  • Loading branch information
Calvin Lobo committed Jul 5, 2024
1 parent 663e3b2 commit f3c6e63
Show file tree
Hide file tree
Showing 4 changed files with 639 additions and 29 deletions.
5 changes: 4 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func New(_fmt string, args ...interface{}) *SimpleError {

// Error satisfies the `error` interface. It uses the `simplerr.Formatter` to generate an error string.
func (e *SimpleError) Error() string {
return Formatter(e)
if parent := e.Unwrap(); parent != nil {
return fmt.Sprintf("%s: %s", e.GetMessage(), parent.Error())
}
return e.msg
}

// Message sets the message text on the error. This message it used to wrap the underlying error, if it exists.
Expand Down
11 changes: 0 additions & 11 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,6 @@ func (s *TestSuite) TestErrorFormatting() {
serr3 := New("something")
s.Equal("something", serr3.Error())

// Change the error formatting style
Formatter = func(e *SimpleError) string {
parent := e.Unwrap()
if parent == nil {
return e.GetMessage()
}
return strings.Join([]string{e.GetMessage(), parent.Error()}, "\n")
}
s.Equal("wrapper 1\noriginal", serr1.Error())
s.Equal("wrapper 2\nwrapper 1\noriginal", serr2.Error())
Formatter = DefaultFormatter
}

func (s *TestSuite) TestStackTrace() {
Expand Down
14 changes: 0 additions & 14 deletions formatters.go

This file was deleted.

Loading

0 comments on commit f3c6e63

Please sign in to comment.