An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 8.0% on proxy.golang.org

proxy.golang.org : github.com/natefinch/filterr

Package filterr makes it easy to filter the errors a function returns. In Go code, it is idiomatic to simply pass up errors that get returned from functions you call (the ubuquitous if err != nil { return err }). However, this practice can have unintended consequences. If your package consistently returns a specific type of error, even if it is undocumented, consumers of your package may write code that relies on that specific error. If you later change the implementation such that a different error is returned, even if the actual behavior of the code is identical, their code will break. This package solves the above problem by declaring exactly what errors a function can return, and enforcing that with code. Any errors which the function is not defined as returning will get wrapped in a generic error which masks the type of the original error, preventing any callers of your function from depending on the error type. Further, the code that enforces the error returns also serves as documentation in the code that is easy to find, since it always resides in a defer statement right at the top of the function body like this: The above function declares itself as only returning errors which either satisfy os.IsNotExist, or are equal to MyError (a custom error type). Any other errors which the function returns will get wrapped in an anonymous error using errors.New. For projects that want to do something other than a simple anonymization of errors, you can customize the transform functions that get called with the errors your function returns using MakeReturnFunc.

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/natefinch/filterr
License: MIT
Latest release: over 11 years ago
First release: over 11 years ago
Namespace: github.com/natefinch
Stars: 4 on GitHub
Forks: 0 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: about 2 months ago

    Loading...
    Readme
    Loading...