英文:
panic handling or what has happend
问题
我不明白如何处理 panic 的最佳方法。我可以编写自己的 panic("bad data or empty source")
,并通过输出确定代码在哪个位置失败,但是对于不是我自己的方法生成的 panic,该怎么办呢?
现在我遇到了这样的错误:
C:/gocode/src/github.com/revel/revel/panic.go:26 (0x4975a4) handleInvocationPanic: c.Response.Out.Write(debug.Stack())
C:/gocode/src/github.com/revel/revel/panic.go:12 (0x4b60ca) PanicFilter.func1: handleInvocationPanic(c, err)
c:/go/src/runtime/asm_amd64.s:437 (0x45cc75) call32: CALLFN(·call32, 32)
c:/go/src/runtime/panic.go:423 (0x42ec17) gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
C:/gocode/src/github.com/revel/revel/intercept.go:93 (0x4b6061) InterceptorFilter.func1: panic(err)
c:/go/src/runtime/asm_amd64.s:437 (0x45cc75) call32: CALLFN(·call32, 32)
c:/go/src/runtime/panic.go:423 (0x42ec17) gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
c:/go/src/runtime/panic.go:42 (0x42d280) panicmem: panic(memoryError)
c:/go/src/runtime/signal_windows.go:161 (0x44233d) sigpanic: panicmem()
C:/gocode/src/github.com/oculus/libs/funcs.go:13 (0x4e0ca5) GetDatesInRange: fmt.Println(err.Error())
C:/gocode/src/github.com/oculus/rest/app/controllers/kpi.go:97 (0x4e3b2f) KpiCtrl.GetNoagg: dates, errors := libs.GetDatesInRange(request.Filters.DayStart, request.Filters.DayEnd) :97 (0x4e9f12)
c:/go/src/runtime/asm_amd64.s:437 (0x45cc75) call32: CALLFN(·call32, 32)
c:/go/src/reflect/value.go:432 (0x471591) Value.call: call(frametype, fn, args, uint32(frametype.size), uint32(retOffset))
c:/go/src/reflect/value.go:300 (0x470258) Value.Call: return v.call("Call", in)
C:/gocode/src/github.com/revel/revel/invoker.go:36 (0x496e51) ActionInvoker: resultValue = methodValue.Call(methodArgs)[0]
C:/gocode/src/github.com/revel/revel/compress.go:47 (0x487f89) CompressFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/intercept.go:103 (0x4954b9) InterceptorFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/oculus/rest/app/init.go:37 (0x4e2366) glob.func1: fc[0](c, fc[1:]) // 执行下一个过滤器阶段。
C:/gocode/src/github.com/revel/revel/i18n.go:155 (0x4947a3) I18nFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/validation.go:191 (0x4ae27d) ValidationFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/flash.go:46 (0x490ee7) FlashFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/session.go:149 (0x4a914c) SessionFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/params.go:133 (0x499116) ParamsFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/filterconfig.go:208 (0x490a64) FilterConfiguringFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/router.go:474 (0x4a61e6) RouterFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/panic.go:15 (0x4972c8) PanicFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/watcher.go:232 (0x4b512d) glob.func31: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/server.go:50 (0x4a6d95) handleInternal: Filters[0](c, Filters[1:])
C:/gocode/src/github.com/revel/revel/server.go:38 (0x4a68f6) handle: handleInternal(w, r, nil)
c:/go/src/net/http/server.go:1422 (0x57cfe1) HandlerFunc.ServeHTTP: f(w, r)
c:/go/src/net/http/server.go:1862 (0x57f285) serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
c:/go/src/net/http/server.go:1361 (0x57ca85) (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
c:/go/src/runtime/asm_amd64.s:1721 (0x45f121) goexit: BYTE $0x90 // NOP
我应该如何处理它们,或者如何确定问题出在哪里。对我来说,这些信息目前并不具有信息量。
英文:
I don't understand what is the best way to handle panics. I can write my own panic("bad data or empty source")
and I'll detetrmine on the output that code fails on this exactly place, but what to do with the panic's that are generated in not mine methods.
Now I have such an error :
C:/gocode/src/github.com/revel/revel/panic.go:26 (0x4975a4) handleInvocationPanic: c.Response.Out.Write(debug.Stack())
C:/gocode/src/github.com/revel/revel/panic.go:12 (0x4b60ca) PanicFilter.func1: handleInvocationPanic(c, err)
c:/go/src/runtime/asm_amd64.s:437 (0x45cc75) call32: CALLFN(·call32, 32)
c:/go/src/runtime/panic.go:423 (0x42ec17) gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
C:/gocode/src/github.com/revel/revel/intercept.go:93 (0x4b6061) InterceptorFilter.func1: panic(err)
c:/go/src/runtime/asm_amd64.s:437 (0x45cc75) call32: CALLFN(·call32, 32)
c:/go/src/runtime/panic.go:423 (0x42ec17) gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
c:/go/src/runtime/panic.go:42 (0x42d280) panicmem: panic(memoryError)
c:/go/src/runtime/signal_windows.go:161 (0x44233d) sigpanic: panicmem()
C:/gocode/src/github.com/oculus/libs/funcs.go:13 (0x4e0ca5) GetDatesInRange: fmt.Println(err.Error())
C:/gocode/src/github.com/oculus/rest/app/controllers/kpi.go:97 (0x4e3b2f) KpiCtrl.GetNoagg: dates, errors := libs.GetDatesInRange(request.Filters.DayStart, request.Filters.DayEnd) :97 (0x4e9f12)
c:/go/src/runtime/asm_amd64.s:437 (0x45cc75) call32: CALLFN(·call32, 32)
c:/go/src/reflect/value.go:432 (0x471591) Value.call: call(frametype, fn, args, uint32(frametype.size), uint32(retOffset))
c:/go/src/reflect/value.go:300 (0x470258) Value.Call: return v.call("Call", in)
C:/gocode/src/github.com/revel/revel/invoker.go:36 (0x496e51) ActionInvoker: resultValue = methodValue.Call(methodArgs)[0]
C:/gocode/src/github.com/revel/revel/compress.go:47 (0x487f89) CompressFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/intercept.go:103 (0x4954b9) InterceptorFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/oculus/rest/app/init.go:37 (0x4e2366) glob.func1: fc[0](c, fc[1:]) // Execute the next filter stage.
C:/gocode/src/github.com/revel/revel/i18n.go:155 (0x4947a3) I18nFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/validation.go:191 (0x4ae27d) ValidationFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/flash.go:46 (0x490ee7) FlashFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/session.go:149 (0x4a914c) SessionFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/params.go:133 (0x499116) ParamsFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/filterconfig.go:208 (0x490a64) FilterConfiguringFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/router.go:474 (0x4a61e6) RouterFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/panic.go:15 (0x4972c8) PanicFilter: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/watcher.go:232 (0x4b512d) glob.func31: fc[0](c, fc[1:])
C:/gocode/src/github.com/revel/revel/server.go:50 (0x4a6d95) handleInternal: Filters[0](c, Filters[1:])
C:/gocode/src/github.com/revel/revel/server.go:38 (0x4a68f6) handle: handleInternal(w, r, nil)
c:/go/src/net/http/server.go:1422 (0x57cfe1) HandlerFunc.ServeHTTP: f(w, r)
c:/go/src/net/http/server.go:1862 (0x57f285) serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
c:/go/src/net/http/server.go:1361 (0x57ca85) (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
c:/go/src/runtime/asm_amd64.s:1721 (0x45f121) goexit: BYTE $0x90 // NOP
How should I work with them, or how to determine where is the trouble. this is not enformative for me for this time.
答案1
得分: 6
在Go语言中,你有可能从panic中恢复,其中包含了panic错误消息。在恢复之后,你可以分析导致panic发生的问题。为了从panic中恢复,你可以使用defer语句,它会延迟执行语句。这意味着你可以跳过一些事件来避免系统崩溃,但这并不是一个很好的解决方案。最好的解决方案是捕获错误并适当地处理它们。如果错误发生在某个第三方框架上,应该由框架的创建者来解决。如果不是,你应该检查为什么你的代码会发生panic。
下面是一个用于panic恢复的代码片段:
defer func() {
if err := recover(); err != nil {
fmt.Printf("Recovered from panic. %s", err)
}
}()
这里是一个简单的例子,展示了如何使用内置的panic和recover方法:
package main
import "fmt"
func badCall() {
panic("Bad call happened!")
}
func test() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Panicking %s\n\r", err)
}
}()
badCall()
fmt.Println("This is never executed!!")
}
func main() {
fmt.Println("Start testing")
test()
fmt.Println("End testing")
}
如果recover在延迟函数中被调用,堆栈停止展开,并且recover返回传递给panic的值(作为interface{}
类型!)。这意味着你可以将一个接口作为参数传递给panic方法,例如:
type ParseError struct {
Index int // 单词列表中的索引。
Word string // 生成解析错误的单词。
Error error // 引发此错误的原始错误(如果有)。
}
// String返回一个可读的错误消息。
func (e *ParseError) String() string {
return fmt.Sprintf("pkg: error parsing %q as int", e.Word)
}
// ... 一些代码
if err != nil {
panic(&ParseError{idx, field, err})
}
// ... 一些代码
然后你可以在延迟语句中分析传递的接口。
阅读这篇文章:
https://github.com/golang/go/wiki/PanicAndRecover
<details>
<summary>英文:</summary>
In `Go` you have the possibility to recover from panic, which does contain the panic error message. After recovering you may analyze the issue why the panic is happening. To recover from panic you can use the `defer` statement which as the name implies, defers execution of the statement. This means that you can skip some events to hang up the system, which is not quite a good solution. The best solution is to capture the errors and handle them adequately. If the error happens on some third party framework this should be addressed by their creators. If not you should check why your code is panicking.
Here a code snippet for panic recover:
defer func() {
if err := recover(); err != nil {
fmt.Printf("Recovered from panic. %s", err)
}
}()
And here is a simple example of how you can use the build in panic and recovery methods:
package main
import "fmt"
func badCall() {
panic("Bad call happend!")
}
func test() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Panicking %s\n\r" , err)
}
}()
badCall()
fmt.Println("This is never executed!!")
}
func main() {
fmt.Println("Start testing")
test()
fmt.Println("End testing")
}
http://play.golang.org/p/Uz9W76SfRT
If recover is called inside a deferred function, the stack stops unwinding and recover returns the value (as an `interface{}`!) that was passed to panic. This means that you can pass an interface as an argument to the panic method, like:
type ParseError struct {
Index int // The index into the space-separated list of words.
Word string // The word that generated the parse error.
Error error // The raw error that precipitated this error, if any.
}
// String returns a human-readable error message.
func (e *ParseError) String() string {
return fmt.Sprintf("pkg: error parsing %q as int", e.Word)
}
// ... some code
if err != nil {
panic(&ParseError{idx, field, err})
}
// ... some code
Then you can analyze the passed interface in the deferred statement.
Read this article:
https://github.com/golang/go/wiki/PanicAndRecover
</details>
# 答案2
**得分**: 1
你可以使用这段代码,并将其放置在每个函数中:
defer func() {
if err := recover(); err != nil {
fmt.Println("发生恐慌并在以下位置恢复,错误信息:", err)
}
}()
尝试一下,它会对你有用的。
<details>
<summary>英文:</summary>
You can use this piece of code and place it in every function
defer func() {
if err := recover(); err != nil {
fmt.Println("Panic Occured and Recovered in, Error Info: ", err)
}
}()
Try this it will work for you.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论