2015-10-09 09:07
浏览 658


Let's say I have some code like this:

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

In case err != nil I will get something like this:

panic: some error explanation here

goroutine 1 [running]:
    /tmp/blabla/main.go:6 +0x80

This stack trace is completely legit but sometimes these error messages may not clarify what happened and so I'd like to dig deeper into the source code of the 3rd party library to investigate what exactly causes this error to be returned. However when my code panics like this, there is no way to get the actual place that returned this error.

A little bit more clarification: as I'm coming from JVM world where exceptions are thrown I can completely trace what exactly line of code thrown the exception and thus can easily find the place and see what gone wrong. Go stack trace ends exactly where my code panics and thus not too useful in my case.

I've created a playground here and ideally I'd like to be able to trace the error to the place it was actually returned from, not panic. (e.g. to line 17, return "", errors.New("some error explanation here"))

Is this even possible?

图片转代码服务由CSDN问答提供 功能建议


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

如果 err!= nil 我会得到这样的东西:

goroutine 1 [运行中]:
 /  tmp / blabla / main.go:6 + 0x80 

此堆栈跟踪完全合法,但有时这些错误消息可能无法阐明发生了什么,因此我想 深入研究第三方库的源代码,以调查究竟是什么原因导致返回此错误。 但是,当我的代码如此恐慌时,无法获得返回此错误的实际位置。

需要更多说明:因为我来自JVM世界,其中存在异常 我可以完全跟踪抛出异常的确切代码行,从而可以轻松找到位置并查看出了什么问题。 Go堆栈跟踪恰好在我的代码恐慌的地方结束,因此在我的情况下不太有用。

我创建了一个游乐场此处,理想情况下,我希望能够将错误跟踪到实际返回错误的位置,而不必担心。 (例如,到第17行,返回“”,errors.New(“此处有一些错误说明”)

这是否可能? \ n

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

  • dqd54099 2015-10-09 10:18

    Shortly: this is not possible. Since errors are values, they are not treated in any special way. Due to this, when function (normally) returns, stack is no more available (ie. another function call may overwrite memory used by returning-error function' stack).

    There is a tool called trace which was introduced with go1.5, but for now, there is no comprehensive tutorial available neither any of those I found says that this kind of feature will be included.

    解决 无用
    打赏 举报

相关推荐 更多相似问题