Skip to content

Commit

Permalink
🐛 Fixed so that it does not panic even if the error is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
nkmr-jp committed Jan 17, 2025
1 parent 13329a2 commit 795c3bb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (l *prettyLogger) logWithError(msg string, level zapcore.Level, err error,
err2 := l.Logger.Output(
4,
l.coloredLevel(level).String()+" "+l.coloredMsg(
fmt.Sprintf("%s%s%s", msg, separator, au.Magenta(err.Error())),
fmt.Sprintf("%s%s%s", msg, separator, au.Magenta(fmt.Sprintf("%v", err))),
level, fields,
),
)
Expand Down
10 changes: 10 additions & 0 deletions pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ func Test_prettyLogger_logWithError(t *testing.T) {
l.logWithError("test message", zapcore.InfoLevel, errors.New("some error"), nil)
assert.Contains(t, buf.String(), "[INTERNAL ERROR] ")
})

t.Run("when error is nil", func(t *testing.T) {
outputType = PrettyOutput
severityLevel = zapcore.DebugLevel

var buf bytes.Buffer
l := newPrettyLogger(&buf, os.Stderr)
l.logWithError("test message", zapcore.InfoLevel, nil, nil)
assert.Contains(t, buf.String(), "<nil>")
})
}

func Test_prettyLogger_coloredLevel(t *testing.T) {
Expand Down

0 comments on commit 795c3bb

Please sign in to comment.