I do sometimes do something similar, but without the check() function.
The exit points are explicit, it is guaranteed that errors will be
wrapped.

    func do(name string) (err error) {
        defer PWrapf(&err, "do(%s)", name)

        s, err := works(name);
        if err != nil {
            return err
        }

        // ...
    }

    // PWrapf applies Wrapf to an error pointer
    func PWrapf(err *error, format string, args ...interface{}) {
        *err = errors.Wrapf(*err, format, args...)
    }

On Sun, 14 Feb 2021 at 01:45, Michael MacInnis
<michael.p.macin...@gmail.com> wrote:
>
> I've been playing around with reducing error handling boilerplate using 
> standard language constructs.
>
> I'm currently doing something that looks like this:
>
>     import (
>         "github.com/michaelmacinnis/handle"
>     )
>
>     func do(name string) (err error) {
>         check, handle := handle.Errorf(&err, "do(%s)", name); defer handle()
>
>         s, err := works(name); check(err)
>
>         // ...
>     }
>
> Other than the named return value and check being a hidden return, are there 
> reasons I would want to avoid doing this? I assume others have tried similar 
> things but I haven't stumbled across any similar packages.
>
> Before using it in production I would probably want a linter that checks to 
> make sure that the statement after handle.Error or handle.Errorf is defer 
> blah, where blah is the name given to the second value returned by those 
> functions and that all of this happens at the start of a function.
>
> Michael.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/cbe53d07-4f99-49fa-a708-dcb85b1aff5bn%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAJ6cK1ZgvpGV_vUFwHc%2BbadJXcoJO%3Dwv4-tfaWTYqjQbM%2BOMMg%40mail.gmail.com.

Reply via email to