proxy.golang.org : github.com/michaelmacinnis/handle
Package handle reduces the boilerplate required for some error handling patterns by (ab)using panic and recover for flow control. See the warnings section for more detail. In functions where the handling of each error is unique or where only a few errors need to be handled, it is best to handle errors with a simple if statement: And, of course, the usual advice about treating errors as values and using the full power of Go to simplify error handling applies. In functions where more than a few errors need to be handled and where all errors will be handled in a similar way, the handle package can be used to ensure consistency while reducing the amount of code dedicated to error handling. If the enclosing function expects to return an error, that error must be a named return value so that the escape.On method and hatch function can be bound to it. The error returned can be wrapped: or returned unmodified: With a deferred hatch, any call to escape.On with a non-nil error will cause the enclosing function to return: An enclosing function can use escape.On to trigger an early return with shared behavior on errors: and it can do so even if the enclosing function does not return an error: Additional error handling actions can be added with handle.Chain as in the example below adapted from Error Handling - Problem Overview: github.com/golang/proposal/blob/master/design/go2draft-error-handling-overview.md Mixing handle with other uses of panic/recover is not recommended. To avoid unhandled panics, the hatch function returned by Error or Errorf must be deferred before any escape.On invocations. The escape.On method must also not be invoked in any goroutine other than the one in which hatch was deferred or by any function outside of the call chain rooted at the function where hatch was deferred. Go's escape analysis can be used as an overly conservative check to ensure that invocations of esape.On occur in the same goroutine and call chain. Set Name to the name given to the escape object, and run, If you see, there is a chance you are doing something that won't end well. Note that this will not detect failure to defer hatch or mixing handle with other uses of panic/recover.
Registry
-
Source
- Documentation
- JSON
purl: pkg:golang/github.com/michaelmacinnis/handle
License: MIT
Latest release: almost 4 years ago
First release: over 4 years ago
Namespace: github.com/michaelmacinnis
Stars: 2 on GitHub
Forks: 0 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: 27 days ago