【GoLang】GoLang 错误处理 -- 异常处理思路示例

代码:

package main

import (
    "fmt"
    //    "testing"
)

var Pkg = "packageName"

type Err struct {
    Pkg  string
    Info string
    Prev error
}

func (e *Err) Error() string {
    if e.Prev == nil {
        return fmt.Sprintf("%s: %s", e.Pkg, e.Info)
    }
    return fmt.Sprintf("%s: %s\n%v", e.Pkg, e.Info, e.Prev)
}

func me(err error, format string, args ...interface{}) *Err {
    if len(args) > 0 {
        return &Err{
            Pkg:  Pkg,
            Info: fmt.Sprintf(format, args...),
            Prev: err,
        }
    }
    return &Err{
        Pkg:  Pkg,
        Info: format,
        Prev: err,
    }
}

func ce(err error, format string, args ...interface{}) {
    if err != nil {
        panic(me(err, format, args...))
    }
}

func ct(err *error) {
    if p := recover(); p != nil {
        if e, ok := p.(error); ok {
            *err = e
        } else {
            panic(p)
        }
    }
}

func oe(e error) error {
    var ret error = e
    for err, ok := ret.(*Err); ok && err.Prev != nil; err, ok = ret.(*Err) {
        ret = err.Prev
    }
    return ret
}

func foo() (err error) {
    defer ct(&err)
    ce(bar(), "call bar")
    return
}

func bar() (err error) {
    defer ct(&err)
    ce(baz(), "call baz")
    return
}

func baz() (err error) {
    return me(nil, "baz")
}

func main() {
    ce(foo(), "call foo")
}

 

输出:

panic: packageName: call foo
packageName: call bar
packageName: call baz
packageName: baz

goroutine 1 [running]:
panic(0x491000, 0xc420016300)
    /opt/golang/go/src/runtime/panic.go:500 +0x1a1
main.ce(0x4f9140, 0xc4200162d0, 0x4a741f, 0x8, 0x0, 0x0, 0x0)
    /opt/golang/workspace/src/github.com/junneyang/letsgo/error_test/error_common.go:40 +0x97
main.main()
    /opt/golang/workspace/src/github.com/junneyang/letsgo/error_test/error_common.go:79 +0x69
Error: process exited with code 2.

 

参考资料:

http://reusee.github.io/post/error-handling/

https://github.com/reusee/codes/blob/master/err/err.go

https://github.com/golang/go/blob/master/src/encoding/gob/error.go

 

posted @ 2016-11-23 12:03  junneyang  阅读(1172)  评论(0编辑  收藏  举报