Skip to content

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

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.

Activity

  1. abhinav commented on Feb 15, 2024

    @abhinav
    Contributor

    Yeah, we should recommend FormatString.

    Although we may want to simplify the output first too #74.
    I think the tree-format is more trouble than it's worth.
    I doubt anybody finds that useful over just empty-line or --- separated traces.

  2. cbandy commented on Jan 29, 2025

    @cbandy
    Contributor

    Sounds related to #59.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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