Skip to content

Commit

Permalink
Don't repeat error causes in error message, support Go 1.20 error int…
Browse files Browse the repository at this point in the history
…erfaces

Co-authored-by: gordon-klotho <[email protected]>
  • Loading branch information
thessem and gordon-klotho committed Jun 14, 2024
1 parent 0922045 commit cc9b274
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 77 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,28 @@ Log a message and 10 fields:

| Package | Time | Time % to zap | Objects Allocated |
| :------ | :--: | :-----------: | :---------------: |
| :zap: zap | 529 ns/op | +0% | 5 allocs/op
| :zap: zap (sugared) | 858 ns/op | +62% | 10 allocs/op
| :zap: :nail_care: zap-prettyconsole | 1904 ns/op | +260% | 12 allocs/op
| :zap: :nail_care: zap-prettyconsole (sugared) | 2372 ns/op | +348% | 17 allocs/op
| :zap: zap | 570 ns/op | +0% | 5 allocs/op
| :zap: zap (sugared) | 861 ns/op | +51% | 10 allocs/op
| :zap: :nail_care: zap-prettyconsole | 2050 ns/op | +260% | 11 allocs/op
| :zap: :nail_care: zap-prettyconsole (sugared) | 2484 ns/op | +336% | 16 allocs/op

Log a message with a logger that already has 10 fields of context:

| Package | Time | Time % to zap | Objects Allocated |
| :------ | :--: | :-----------: | :---------------: |
| :zap: zap | 52 ns/op | +0% | 0 allocs/op
| :zap: zap (sugared) | 64 ns/op | +23% | 1 allocs/op
| :zap: :nail_care: zap-prettyconsole | 1565 ns/op | +2910% | 7 allocs/op
| :zap: :nail_care: zap-prettyconsole (sugared) | 1637 ns/op | +3048% | 8 allocs/op
| :zap: zap | 57 ns/op | +0% | 0 allocs/op
| :zap: zap (sugared) | 67 ns/op | +18% | 1 allocs/op
| :zap: :nail_care: zap-prettyconsole | 1705 ns/op | +2891% | 6 allocs/op
| :zap: :nail_care: zap-prettyconsole (sugared) | 1712 ns/op | +2904% | 7 allocs/op

Log a static string, without any context or `printf`-style templating:

| Package | Time | Time % to zap | Objects Allocated |
| :------ | :--: | :-----------: | :---------------: |
| :zap: :nail_care: zap-prettyconsole | 36 ns/op | -16% | 0 allocs/op
| :zap: zap | 43 ns/op | +0% | 0 allocs/op
| :zap: zap (sugared) | 58 ns/op | +35% | 1 allocs/op
| :zap: :nail_care: zap-prettyconsole (sugared) | 62 ns/op | +44% | 1 allocs/op
| :zap: :nail_care: zap-prettyconsole | 39 ns/op | -11% | 0 allocs/op
| :zap: zap | 44 ns/op | +0% | 0 allocs/op
| :zap: zap (sugared) | 60 ns/op | +36% | 1 allocs/op
| :zap: :nail_care: zap-prettyconsole (sugared) | 62 ns/op | +41% | 1 allocs/op

Released under the [MIT License](LICENSE.txt)

Expand Down
117 changes: 68 additions & 49 deletions encoder_test.go

Large diffs are not rendered by default.

52 changes: 37 additions & 15 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package prettyconsole
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"

"github.com/pkg/errors"
)

var reErrorJoins = regexp.MustCompile(`[\s,:;\\n]+$`)

// Encodes the given error into fields of an object. A field with the given
// name is added for the error message.
//
Expand All @@ -30,10 +33,6 @@ func (e *prettyConsoleEncoder) encodeError(key string, err error) (retErr error)
enc := e.clone()
enc.OpenNamespace(key)

enc.colorizeAtLevel("=")
enc.namespaceIndent += 1
enc.inList = true

// Try to capture panics (from nil references or otherwise) when calling
// the Error() method
defer func() {
Expand All @@ -55,24 +54,47 @@ func (e *prettyConsoleEncoder) encodeError(key string, err error) (retErr error)
e.listSep = e.cfg.LineEnding + strings.Repeat(" ", e.namespaceIndent)
}()

var causes []error
switch et := err.(type) {
case interface{ Errors() []error }:
causes = et.Errors()
case interface{ Unwrap() []error }:
causes = et.Unwrap()
case interface{ Cause() error }:
causes = []error{et.Cause()}
case interface{ Unwrap() error }:
causes = []error{et.Unwrap()}
}

basic := err.Error()
enc.addSafeString(basic)
for _, cause := range causes {
if cause != nil {
cbasic := cause.Error()
basic, _, _ = strings.Cut(basic, cbasic)
// TrimSuffix with seperator characters like : or , surrounded by
// any number of spaces
basic = reErrorJoins.ReplaceAllString(strings.TrimSpace(basic), "")
}
}
if basic != "" {
enc.namespaceIndent += 1
enc.colorizeAtLevel("=")
enc.inList = true
enc.addSafeString(basic)
}

// Write causes recursively
skipDetail := false
switch et := err.(type) {
case interface{ Cause() error }:
if err := enc.encodeError("cause", et.Cause()); err != nil {
for i, ei := range causes {
if len(causes) > 1 {
key = "cause." + strconv.Itoa(i)
} else {
key = "cause"
}
if err := enc.encodeError(key, ei); err != nil {
return err
}
skipDetail = true
case interface{ Errors() []error }:
for i, ei := range et.Errors() {
if err := enc.encodeError("cause."+strconv.Itoa(i), ei); err != nil {
return err
}
skipDetail = true
}
}

// If there's a stacktrace, print it. If this error is a formatter, we'll
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/Code-Hex/dd v1.1.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.9.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
Expand Down
Binary file modified internal/readme/images/Configuration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified internal/readme/images/Errors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified internal/readme/images/Formatting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified internal/readme/images/Object.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified internal/readme/images/Reflection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified internal/readme/images/Simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified internal/readme/images/SingleLine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified internal/readme/images/ZapConsole.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cc9b274

Please sign in to comment.