Skip to content

Latest commit

 

History

History
61 lines (49 loc) · 1.36 KB

go2errorhanding.md

File metadata and controls

61 lines (49 loc) · 1.36 KB

Please comment here.

When I first saw this proposal, I immediately thought handle f(), just like defer f(), but I don't intend to think deep about it.

This article is about another issue, I often need to wrap the error.

My current practice:

import "github.com/pkg/errors"
func fn() error {
    img, err := fetchImg()
    if err != nil {
        return errors.WithMessage(err, "download fail")
    }
    err = save(img)
    if err != nil {
        return errors.WithMessage(err, "save fail")
    }
    // ...
}

Rewrite as Go2 draft:

import "github.com/pkg/errors"
func fn() error {
    handle err {
        return err
    }
    img, err := fetchImg()
    check errors.WithMessage(err, "download fail")
    err = save(img)
    check errors.WithMessage(err, "save fail")
    // ...
}

This is not simple enough. How to make it simpler?

My first improvement suggestion:

import "github.com/pkg/errors"
func fn() error {
    handle err error, msg string {
        return errors.WithMessage(err, msg)
    }
    img := check("download fail") fetchImg()
    check("save fail") save(img)
    // ...
}

My second improvement suggestion:

There is no second one, because I have thought of several ways of writing, I feel that they are bad.

Please comment here.