Skip to content

Error traces are not printed with fmt %+v when outer error is not errTrace #89

@prashantv

Description

@prashantv

While writing some tests with errtrace, I had a scenario where the formatted error didn't have an error trace, even though underlying errors were wrapped.

Example snippet,

err1 := errtrace.Wrap(errors.New("err1"))
err2 := errtrace.Wrap(fmt.Errorf("err2: %w", err1))
err3 := fmt.Errorf("err3: %w", err2)
fmt.Printf("using fmt format:\n%+v\n========\nFormat:\n%v", err3, errtrace.FormatString(err3))

gives:

using fmt format:
err3: err2: err1
========
Format:
err3: err2: err1

main.main
	/tmp/sandbox1006302431/prog.go:13
main.main
	/tmp/sandbox1006302431/prog.go:14

Since the last error was not wrapped with errtrace, the %+v has no impact. However, FormatString does work correctly in this case.

While we can't fix this, is this gotcha worth calling out somewhere, and should we recommend errtrace.FormatString over %+v.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions