Skip to content

workflow

Michael Kenney edited this page Aug 14, 2018 · 7 revisions
  1. start

    var errs errors.Err
    var err error
  2. error occurs in come code somewhere

    err = someFunc()
  3. capture and wrap the error to add context

    if nil != err {
        errs = errors.Wrap(err, 0, "the call to someFunc to do some particular work failed")
    }
  4. do some more work, repeat

    // if someFunc failed do a thing
    if nil != err {
        errs = errors.Wrap(err, 0, "the call to someFunc while trying to do allTheWork failed")
        err = buidSome()
        if nil != err {
            errs = errors.Wrap(err, 0, "could not build 'some'")
        }
    }
  5. return standard error

    func allTheStuff() error {
        var errs errors.Err
        var err error
    
        err = someFunc()
        // if someFunc failed do a thing
        if nil != err {
            errs = errors.Wrap(err, 0, "the call to someFunc while trying to do allTheWork failed")
            err = buidSome()
            if nil != err {
                errs = errors.Wrap(err, FailedToBuild, "could not build 'some'")
            }
        }
        return errs
    }
  6. capture and handle

    func main() {
        errs := allTheStuff()
        for _, err := range errs.(errors.Err) {
            switch err.Err().(type) {
            case BuildErr:
                   log.Fatalf("%+v", errs)
            }
        }
        log.Errorf("%+v", errs)
    }
Clone this wiki locally