27 Star 83 Fork 11

lunny / tango

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
recovery.go 990 Bytes
一键复制 编辑 原始数据 按行查看 历史
lunny 提交于 2017-03-07 21:27 . lint fixed
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tango
import (
"bytes"
"fmt"
"net/http"
"runtime"
)
// Recovery returns a middleware which catch panics and log them
func Recovery(debug bool) HandlerFunc {
return func(ctx *Context) {
defer func() {
if e := recover(); e != nil {
var buf bytes.Buffer
fmt.Fprintf(&buf, "Handler crashed with error: %v", e)
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
} else {
fmt.Fprintf(&buf, "\n")
}
fmt.Fprintf(&buf, "%v:%v", file, line)
}
var content = buf.String()
ctx.Logger.Error(content)
if !ctx.Written() {
if !debug {
ctx.Result = InternalServerError(http.StatusText(http.StatusInternalServerError))
} else {
ctx.Result = InternalServerError(content)
}
}
}
}()
ctx.Next()
}
}
Go
1
https://gitee.com/lunny/tango.git
git@gitee.com:lunny/tango.git
lunny
tango
tango
master

搜索帮助