proxy.golang.org : github.com/mpvl/errd
Package errd simplifies error and defer handling. Package errd allows returning form a block of code without using the usual if clauses, while properly intercepting errors and passing them to code called at defer time. The following piece of idiomatic Go writes the contents of a reader to a file on Google Cloud Storage: Google Cloud Storage allows files to be written atomically. This code minimizes the chance of writing a bad file by aborting the write, using CloseWithError, whenever any anomaly is encountered. This includes a panic that could occur in the reader. Package errd aims to reduce bugs resulting from such subtleties by making the default of having very strict error checking easy. The following code achieves the same as the above: Discard is an example of an error handler. Here it signals that we want to ignore the error of the first Close. In all of the code above we made the common faux pas of passing errors on without decorating them. Package errd defines a Handler type to simplify the task of decorating. Suppose we want to use github.com/pkg/errors to decorate errors. A simple handler can be defined as: This handler can then be used as follows: The storage package used in this example defines the errors that are typically a result of user error. It would be possible to write a more generic storage writer that will add additional clarification when possible. Using such a handler as a default handler would look like: Setting up a global config with a default handler and using that everywhere makes it easy to enforce decorating errors. Error handlers can also be used to pass up HTTP error codes, log errors, attach metrics, etc. A function that is passed to Run does have any return values, not even an error. Users are supposed to set return values in the outer scope, for example by using named return variables. If name is only set at the end, any error will force an early return from Run and leave name empty. Otherwise, name will be set and err will be nil.
Registry
-
Source
- Documentation
- JSON
purl: pkg:golang/github.com/mpvl/errd
License: BSD-3-Clause
Latest release: almost 8 years ago
First release: almost 8 years ago
Namespace: github.com/mpvl
Stars: 158 on GitHub
Forks: 6 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: 21 days ago