How to get the stack trace pointing to actual error reason

前端 未结 8 1686
夕颜
夕颜 2020-12-18 19:42

Let\'s say I have some code like this:

value, err := some3rdpartylib.DoSomething()
if err != nil {
    panic(err)
}

In case err != ni

相关标签:
8条回答
  • 2020-12-18 20:12
    package main
    
    import (
        "errors"
        "fmt"
    )
    
    func main() {
        value, err := DoSomething()
        if err != nil {
            panic(err)
        }
        fmt.Println(value)
    }
    
    func DoSomething() (string, error) {
        return "", errors.New("some error explanation here")
    }
    

    The problem is the standard package errors does not attach the stack trace at the point it occurs. You can use github.com/pkg/errors to do that:

    package main
    
    import (
        "github.com/pkg/errors"
        "fmt"
    )
    
    func main() {
        value, err := DoSomething()
        if err != nil {
            fmt.Printf("%+v", err)
        }
        fmt.Println(value)
    }
    
    func DoSomething() (string, error) {
        return "", errors.New("some error explanation here")
    }
    
    $ go run stacktrace.go
    some error explanation here
    main.DoSomething
        /Users/quanta/go/src/github.com/quantonganh/errors/stacktrace.go:18
    main.main
        /Users/quanta/go/src/github.com/quantonganh/errors/stacktrace.go:10
    runtime.main
        /usr/local/Cellar/go/1.15.2/libexec/src/runtime/proc.go:204
    runtime.goexit
        /usr/local/Cellar/go/1.15.2/libexec/src/runtime/asm_amd64.s:1374
    

    More details: https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package

    0 讨论(0)
  • 2020-12-18 20:17

    Take a look at https://github.com/ztrue/tracerr

    I created this package in order to have both stack trace and source fragments to be able to debug faster and log errors with much more details.

    Here is a code example:

    package main
    
    import (
        "io/ioutil"
        "github.com/ztrue/tracerr"
    )
    
    func main() {
        if err := read(); err != nil {
            tracerr.PrintSourceColor(err)
        }
    }
    
    func read() error {
        return readNonExistent()
    }
    
    func readNonExistent() error {
        _, err := ioutil.ReadFile("/tmp/non_existent_file")
        // Add stack trace to existing error, no matter if it's nil.
        return tracerr.Wrap(err)
    }
    

    And here is the output:

    0 讨论(0)
提交回复
热议问题