Skip to content

Commit

Permalink
Improved the formatting and reliability of the stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterclaerhout committed Mar 24, 2020
1 parent 2da210e commit 9f17fd5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"go.testFlags": [
"-v"
]
}
17 changes: 14 additions & 3 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"github.com/go-errors/errors"
"github.com/pkg/errors"
"github.com/sanity-io/litter"

"github.com/pieterclaerhout/go-formatter"
Expand Down Expand Up @@ -199,10 +199,21 @@ func StackTrace(err error) {

// FormattedStackTrace returns a formatted stacktrace for err
func FormattedStackTrace(err error) string {
if cause := causeOfError(err); cause != nil {

if cause := errors.Cause(err); cause != nil {
err = cause
}
return strings.TrimSpace(errors.Wrap(err, 2).ErrorStack())

type stackTracer interface {
StackTrace() errors.StackTrace
}

if err, ok := err.(stackTracer); ok {
return strings.TrimSpace(fmt.Sprintf("%+v", err))
}

return strings.TrimSpace(fmt.Sprintf("%+v", err))

}

// Fatal logs a fatal error message to stdout and exits the program with exit code 1
Expand Down
22 changes: 0 additions & 22 deletions logger_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"fmt"
"os"
"strings"
"sync"
"time"

"github.com/fatih/color"
)

var logMutex = &sync.Mutex{}

var colors = map[string]*color.Color{
"DEBUG": color.New(color.FgHiBlack),
"INFO ": color.New(color.FgHiGreen),
Expand Down Expand Up @@ -65,7 +62,6 @@ func printMessage(level string, message string) {
}

if PrintColors {
// cw := colorable.NewColorable(w)
if c, ok := colors[level]; ok {
c.Fprintln(w, message)
return
Expand All @@ -75,21 +71,3 @@ func printMessage(level string, message string) {
w.Write([]byte(message + "\n"))

}

func causeOfError(err error) error {

type causer interface {
Cause() error
}

for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}

return err

}
30 changes: 0 additions & 30 deletions logger_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -96,35 +95,6 @@ func TestPrintMessage(t *testing.T) {

}

type customError struct {
message string
}

var errCustomErrorCause = errors.New("cause of error")

func newCustomError(message string) *customError {
return &customError{
message: message,
}
}

func (e *customError) Error() string {
return e.message
}

func (e *customError) Cause() error {
return errCustomErrorCause
}

func TestCauseOfError(t *testing.T) {

err := newCustomError("custom error")
cause := causeOfError(err)

assert.EqualValues(t, errCustomErrorCause, cause)

}

func resetLogConfig() {
PrintTimestamp = false
DebugMode = false
Expand Down
29 changes: 26 additions & 3 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,36 @@ func TestStackTrace(t *testing.T) {
actualStdErr := stderr.String()

assert.Equal(t, "", actualStdOut)
assert.True(t, strings.HasPrefix(actualStdErr, "test | ERROR | *errors.fundamental my error\n"))
assert.True(t, strings.HasPrefix(actualStdErr, "test | ERROR | my error\n"))

}

type CustomError struct{}

func (m *CustomError) Error() string {
return "boom"
}

func Test_StackTraceCustom(t *testing.T) {

resetLogConfig()
stdout, stderr := redirectOutput()
defer resetLogOutput()

log.StackTrace(&CustomError{})

actualStdOut := stdout.String()
actualStdErr := stderr.String()

assert.Equal(t, "", actualStdOut, "stdout")
t.Log(actualStdErr)
assert.True(t, strings.HasPrefix(actualStdErr, "test | ERROR | boom\n"), "stderr")

}

func TestFormattedStackTrace(t *testing.T) {
actual := log.FormattedStackTrace(errors.New("my error"))
assert.True(t, strings.HasPrefix(actual, "*errors.fundamental my error\n"))
assert.True(t, strings.HasPrefix(actual, "my error\n"))
}

func TestFatal(t *testing.T) {
Expand Down Expand Up @@ -564,7 +587,7 @@ func TestCheckError(t *testing.T) {
{"nil-debug-color", nil, true, "", "", -1},

{"err-release-nocolor", errors.New("test"), false, "", "test | FATAL | test\n", 1},
{"err-debug-nocolor", errors.New("test"), true, "", "test | FATAL | *errors.fundamental test\n", 1},
{"err-debug-nocolor", errors.New("test"), true, "", "test | FATAL | test\n", 1},
}

for _, tc := range tests {
Expand Down

0 comments on commit 9f17fd5

Please sign in to comment.