Skip to content

proposal: Go 2 error values #29934

Closed
Closed

Description

@jba

This issue is for discussion of our Go 2 error values proposal, which is based on our draft designs for error information and formatting.

This proposal will follow the process outlined in the "Go 2, here we come" blog post: we will have everything ready to go and checked in at the start of the Go 1.13 cycle (Feb 1), we will spend the next three months using those features and soliciting feedback based on actual usage, and then at the start of the release freeze (May 1), we will make the "launch decision" about whether to include the work in Go 1.13 or not.

Update 2019-05-06: See launch decision here: #29934 (comment)

Activity

  1. added this to the Proposal milestone on Jan 25, 2019
  2. mvdan commented on Jan 25, 2019

    @mvdan
    Member

    Assuming this is accepted and implemented before generics, would the design be adapted in a potential redesign of std with generics?

  3. mpvl commented on Jan 25, 2019

    @mpvl
    Contributor

    @mvdan We think the design not using generics resulted in a slightly nicer API in the end. So we will probably not do so. That said, it can't be ruled out there will be additional API using generics.

  4. networkimprov commented on Jan 25, 2019

    @networkimprov

    Errors constructed with errors.New and fmt.Errorf will display differently with %+v

    Could a change in output break consumers which compare it with a hard-coded string, e.g. test code?

  5. neild commented on Jan 26, 2019

    @neild
    Contributor

    Errors constructed with errors.New and fmt.Errorf will display differently with %+v

    Could a change in output break consumers which compare it with a hard-coded string, e.g. test code?

    Conceivable, but since the fmt package currently treats %+v identically to %v for any type which implements the error interface there isn't a strong reason for users to use the former to format errors at the moment.

  6. Sherlock-Holo commented on Jan 27, 2019

    @Sherlock-Holo

    I wonder why xerrors.Errorf("new error: %w", baseErr) %w must at the end, I think when print the error, it should be

    baseErr: some error

    not

    some error: baseErr

  7. networkimprov commented on Jan 27, 2019

    @networkimprov

    Agreed it's odd that the format string is an output-type controller, rather than a flag in the verb like %$v.

    Also, have the authors considered providing this, which is conspicuously absent in package fmt:

    func Error(a ...interface{}) error
    

    This works like errors.New(fmt.Sprint(a, b))
    Just as Errorf works like errors.New(fmt.Sprintf(s, a, b))

  8. neild commented on Jan 27, 2019

    @neild
    Contributor

    Agreed it's odd that the format string is an output-type controller, rather than a flag in the verb like %$v.

    What are the arguments for a flag over a verb? I don't see a strong argument for one over the other and %w has some mnemonic value, but perhaps I'm missing something.

    func Error(a ...interface{}) error

    I think this is orthogonal to the rest of the proposal, but it doesn't seem unreasonable. On the other hand, I don't think I've ever felt the absence of this function.

  9. networkimprov commented on Jan 28, 2019

    @networkimprov

    I'm referring to this output-type control via the format string: "if ... the format string ends with : %s, : %v, or : %w, then the returned error will implement FormatError." Why not a flag for %s & %v?

    The easiest way to print an error is: fmt.Println("this happened: ", err)
    The easiest way to make an error should be: fmt.Error("this happened: ", err)

  10. jba commented on Jan 28, 2019

    @jba
    ContributorAuthor

    Why not a flag for %s & %v?

    The only existing flags that don't have a meaning for %s and %v are space and 0. But those both feel wrong, so we'd need to make up a new flag. That means one could potentially use that flag with any verb, but it would be meaningless except for %s and %v. That feels wasteful—flags and verbs should combine orthogonally to (almost) always produce a useful result (with space and 0 themselves being the notable exceptions).

  11. ericlagergren commented on Jan 28, 2019

    @ericlagergren
    Contributor

    To be clear: does this mean each call to fmt.Errorf will collect a stack trace?

  12. neild commented on Jan 28, 2019

    @neild
    Contributor

    To be clear: does this mean each call to fmt.Errorf will collect a stack trace?

    A single stack frame, not a full trace.

  13. tandr commented on Jan 28, 2019

    @tandr

    To be clear: does this mean each call to fmt.Errorf will collect a stack trace?

    A single stack frame, not a full trace.

    Would it be possible to make it "adjustable" somehow? In our home-made logging wrapper we have noticed that we have to pull a couple or 3 frames to get to "the caller of interest", so printout looks more or less pointing to the place where logwrap.Print(err) function was called.
    If this is becoming a part of the standard, in case when wrapper (or utility method) is created to simplify an error creation, I would like to report the place where that method was called from, and not the utility method itself.

    We did it by (arguable not so elegant) way as adding a parameter "how big is the stack offset" to the logging function, but I am not sure if that's the only option here. Adding special format symbol for stack, with number of frames interesting might be one of possible approaches.

  14. 445 remaining items

  15. fatih commented on Feb 26, 2020

    @fatih
    Member

    Thank you @changkun for the links.

    @jba @rsc based on the links that @changkun provided, can we assume that error handling work is suspended (or paused)? If that is the case we should update the issue with a notice reflecting the latest status. There are many people (including me) assuming this is still and on-going work.

    I was assuming that we would receive some of the functionality, such as stack frames and was planning accordingly for that internally at GitHub. I need to revisit my planning and inform teams at GitHub that the work on errors is delayed (or suspended) if that is the case.

  16. fatih commented on Feb 26, 2020

    @fatih
    Member

    @JavierZunzunegui thanks for the link. Is there any additional benefit compared to xerrors, which already includes frame info ?

  17. JavierZunzunegui commented on Feb 26, 2020

    @JavierZunzunegui

    @JavierZunzunegui thanks for the link. Is there any additional benefit compared to xerrors, which already includes frame info ?

    (+) It provides much more formatting flexibility. Skip want you want, encode the others any way you want, if you want to you can even reverse the encoding order.
    (+) Mixing non-zerrors with zerrors fares better than mixing non-xerrors with xerrors. Formatting only messes up locally (versus messing up the entire rest of the error in xerrors), and you always have the option to skip ayways (not so in xerrors).
    (+) Easier to use. Your errors don't have to implement anything besides Error() string, the library provides everything else.
    (?) performance haven't done the benchmark comparison yet, but will do.
    (?) minimal API. Comes with being new, it makes it easier to understand but the desired 'utilities' are yet to come, with usage feedback.
    (-) new and untested in production. You won't find libraries using it (yet?).
    (-) v0.1.0. I'll try not to break things but that's not set in stone...

  18. flibustenet commented on Feb 26, 2020

    @flibustenet

    About including frame, there are a lot of lib implementing this, and it's very easy to do, i did it myself to. But all of them are unusable in library if there is not at least a common interface to know if an error already embed the frame and how to print it.

  19. JavierZunzunegui commented on Feb 26, 2020

    @JavierZunzunegui

    About including frame, there are a lot of lib implementing this, and it's very easy to do, i did it myself to. But all of them are unusable in library if there is not at least a common interface to know if an error already embed the frame and how to print it.

    @flibustenet of course, the ideal place for any such solution is the standard library, but that is not happenning any time soon. We are in the same place with frames / formatting that we were with wrapping pre-1.13: multiple, sometimes poorly compatible frameworks. But that is the way to experiment and ultimately show, through experience, what is best, and guide the decision of what makes it to the standard library. That was very much the story of wrapping too.

  20. knz commented on Feb 26, 2020

    @knz

    The issue with zerrors linked above is that the error objects are not suitable for transporting over the wire in a distributed application. It's not possible to reliably inspect stack traces and frame information across servers.

    See crdb's own errors replacement which supports network transport and is battle-tested in production.

  21. knz commented on Feb 26, 2020

    @knz

    the crdb errors package also has its own formatting protocol for %+v which aims to fix many shortcomings of xerrors.

  22. ianlancetaylor commented on Feb 27, 2020

    @ianlancetaylor
    Contributor

    @changkun We are unlikely to do anything about error handling any time soon. But this proposal is not about error handling. It is about error values. They are different things. See, e.g., the separate discussions of error values and error handling at https://go.googlesource.com/proposal/+/master/design/go2draft.md.

  23. changkun commented on Feb 27, 2020

    @changkun
  24. ghost deleted a comment from on Jan 10, 2022
  25. gophun commented on Jun 6, 2022

    @gophun

    Shouldn't this issue be closed? The proposal was accepted and shipped with Go 1.13. As @ianlancetaylor pointed out this issue was about the error values proposal, not about error handling in general. This is currently the most commented open issue, so I was expecting something interesting going on here, but it's already done.

  26. locked and limited conversation to collaborators on Jun 6, 2023
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

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ProposalProposal-Acceptedv2An incompatible library change

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions