Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go2 error handling for error-tree #32099

Closed
Konstantin8105 opened this issue May 17, 2019 · 6 comments
Closed

proposal: Go2 error handling for error-tree #32099

Konstantin8105 opened this issue May 17, 2019 · 6 comments
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge Proposal v2 A language change or incompatible library change
Milestone

Comments

@Konstantin8105
Copy link
Contributor

Please add into wiki Go2ErrorHandling, my proposal for error handling in error-tree. Preview:

func printSum(a, b string) error {
  // create error tree
  et := errors.New("check input data")
  errorhandling(et interface{Add(error)}){ // add type just for clearification
    x, et := strconv.Atoi(a)
    y, et := strconv.Atoi(b)
  }
  // is error-tree have errors
  if et.IsError() {
    return et
  }
  fmt.Println("result:", x + y)
  return nil
}

In detail see: Go2ErrorTree

@gopherbot gopherbot added this to the Proposal milestone May 17, 2019
@ianlancetaylor
Copy link
Contributor

CC @jba @neild

@jba
Copy link
Contributor

jba commented May 17, 2019

It looks like there are two ideas here. One is an "error tree" type that can collect multiple errors. It seems that this type is itself an error, but it can also represent the state of there being no errors. So it is already incompatible with the basic property of Go error handling, that there is an error if and only if err != nil.

The second idea is some new syntax for automatically collecting errors into an error tree. That results in simpler control flow (no if err checks) for code where several actions are taken independently. I believe that is rare, however; it's much more common for an action to depend on the result of a previous action. In those cases, the special syntax will obscure bugs. For example, in this code:

errorhandling(et interface{Add(error)}) {
   a, et := f()
   b, et := g(a)
}

we erroneously pass a garbage a to g when f returns an error.

@Konstantin8105
Copy link
Contributor Author

Yes, @jba, you are right. Syntax errorhandling is work fine, only if expessions inside are independancy.
And you example looks like(it is valid code see https://play.golang.org/p/xl5dRJgvKqU):

  a, err := f() 
  // no error checking
  b, err := g(a)
  if err != nil{
    return err
  }

but in that case we will not see error from function f(). If we use errorhandling, then error will garantee added in error-tree.

@networkimprov
Copy link

@Konstantin8105 what are the problems and solution requirements that motivated your proposal?

FYI, I've collected a menu of Requirements to Consider for Go 2 Error Handling.

Also, please feel free to add a link to your proposal to the wiki:
https://github.com/golang/go/wiki/Go2ErrorHandlingFeedback

@Konstantin8105
Copy link
Contributor Author

Konstantin8105 commented May 20, 2019

@rsc rsc added the v2 A language change or incompatible library change label May 28, 2019
@ianlancetaylor
Copy link
Contributor

Thanks for this proposal. This combines error handling and error values into a single proposal. But the idea is somewhat complicated: it requires a builtin type more complicated than error, and it requires additional control within the errorhandling construct. It also seems to implicitly call an Add method, which is not something we ever do in the language today.

We aren't going to adopt this idea.

@bradfitz bradfitz added the error-handling Language & library change proposals that are about error handling. label Oct 29, 2019
@golang golang locked and limited conversation to collaborators Oct 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

7 participants