Skip to content

Commit

Permalink
Merge pull request #8 from teru01/feature/error-logs
Browse files Browse the repository at this point in the history
improve stack trace logs with %+v
  • Loading branch information
m-mizutani authored May 12, 2024
2 parents 2bd45f5 + 8ede9d2 commit 684b8eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
10 changes: 9 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,15 @@ func (x *Error) Format(s fmt.State, verb rune) {
case 'v':
if s.Flag('+') {
_, _ = io.WriteString(s, x.Error())
x.st.Format(s, verb)
var c *Error
for c = x; c.Unwrap() != nil; {
cause, ok := c.Unwrap().(*Error)
if !ok {
break
}
c = cause
}
c.st.Format(s, verb)
return
}
fallthrough
Expand Down
19 changes: 12 additions & 7 deletions examples/stacktrace_print/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package main

import (
"errors"
"log"
"os"

"github.com/m-mizutani/goerr"
)

func someAction(fname string) error {
if _, err := os.Open(fname); err != nil {
return goerr.Wrap(err, "failed to open file")
}
return nil
func nestedAction2() error {
return errors.New("fatal error in the nested action2")
}

func nestedAction() error {
return goerr.Wrap(nestedAction2(), "nestedAction2 failed")
}

func someAction() error {
return goerr.Wrap(nestedAction(), "nestedAction failed")
}

func main() {
if err := someAction("no_such_file.txt"); err != nil {
if err := someAction(); err != nil {
log.Fatalf("%+v", err)
}
}

0 comments on commit 684b8eb

Please sign in to comment.