1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 17:21:02 +00:00

Support custome recover func fix #2004

This commit is contained in:
astaxie 2016-09-15 12:16:24 +08:00
parent c16507607c
commit 421bf97b84
2 changed files with 38 additions and 32 deletions

View File

@ -19,9 +19,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime"
"strings" "strings"
"github.com/astaxie/beego/config" "github.com/astaxie/beego/config"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
@ -34,6 +36,7 @@ type Config struct {
RouterCaseSensitive bool RouterCaseSensitive bool
ServerName string ServerName string
RecoverPanic bool RecoverPanic bool
RecoverFunc func(*context.Context)
CopyRequestBody bool CopyRequestBody bool
EnableGzip bool EnableGzip bool
MaxMemory int64 MaxMemory int64
@ -142,6 +145,37 @@ func init() {
} }
} }
func recoverPanic(ctx *context.Context) {
if err := recover(); err != nil {
if err == ErrAbort {
return
}
if !BConfig.RecoverPanic {
panic(err)
}
if BConfig.EnableErrorsShow {
if _, ok := ErrorMaps[fmt.Sprint(err)]; ok {
exception(fmt.Sprint(err), ctx)
return
}
}
var stack string
logs.Critical("the request url is ", ctx.Input.URL())
logs.Critical("Handler crashed with error", err)
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
logs.Critical(fmt.Sprintf("%s:%d", file, line))
stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
}
if BConfig.RunMode == DEV {
showErr(err, ctx, stack)
}
}
}
func newBConfig() *Config { func newBConfig() *Config {
return &Config{ return &Config{
AppName: "beego", AppName: "beego",
@ -149,6 +183,7 @@ func newBConfig() *Config {
RouterCaseSensitive: true, RouterCaseSensitive: true,
ServerName: "beegoServer:" + VERSION, ServerName: "beegoServer:" + VERSION,
RecoverPanic: true, RecoverPanic: true,
RecoverFunc: recoverPanic,
CopyRequestBody: false, CopyRequestBody: false,
EnableGzip: false, EnableGzip: false,
MaxMemory: 1 << 26, //64MB MaxMemory: 1 << 26, //64MB

View File

@ -626,7 +626,9 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
context.Reset(rw, r) context.Reset(rw, r)
defer p.pool.Put(context) defer p.pool.Put(context)
defer p.recoverPanic(context) if BConfig.RecoverFunc != nil {
defer BConfig.RecoverFunc(context)
}
context.Output.EnableGzip = BConfig.EnableGzip context.Output.EnableGzip = BConfig.EnableGzip
@ -878,37 +880,6 @@ func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo
return return
} }
func (p *ControllerRegister) recoverPanic(context *beecontext.Context) {
if err := recover(); err != nil {
if err == ErrAbort {
return
}
if !BConfig.RecoverPanic {
panic(err)
}
if BConfig.EnableErrorsShow {
if _, ok := ErrorMaps[fmt.Sprint(err)]; ok {
exception(fmt.Sprint(err), context)
return
}
}
var stack string
logs.Critical("the request url is ", context.Input.URL())
logs.Critical("Handler crashed with error", err)
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
logs.Critical(fmt.Sprintf("%s:%d", file, line))
stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
}
if BConfig.RunMode == DEV {
showErr(err, context, stack)
}
}
}
func toURL(params map[string]string) string { func toURL(params map[string]string) string {
if len(params) == 0 { if len(params) == 0 {
return "" return ""