wrap panic

Signed-off-by: runzexia <runzexia@yunify.com>
This commit is contained in:
runzexia
2019-05-11 11:47:46 +08:00
committed by zryfish
parent 6dd8622136
commit 8edcd0c355

View File

@@ -18,6 +18,7 @@
package app
import (
"bytes"
goflag "flag"
"fmt"
"github.com/golang/glog"
@@ -37,6 +38,7 @@ import (
"kubesphere.io/kubesphere/pkg/simple/client/devops_mysql"
"log"
"net/http"
goRuntime "runtime"
)
var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary
@@ -74,7 +76,7 @@ func Run(s *options.ServerRunOptions) error {
container := runtime.Container
container.DoNotRecover(false)
container.Filter(filter.Logging)
container.RecoverHandler(logStackOnRecover)
for _, webservice := range container.RegisteredWebServices() {
for _, route := range webservice.Routes() {
log.Println(route.Method, route.Path)
@@ -195,3 +197,19 @@ func waitForResourceSync() {
log.Println("resources sync success")
}
func logStackOnRecover(panicReason interface{}, httpWriter http.ResponseWriter) {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("recover from panic situation: - %v\r\n", panicReason))
for i := 2; ; i += 1 {
_, file, line, ok := goRuntime.Caller(i)
if !ok {
break
}
buffer.WriteString(fmt.Sprintf(" %s:%d\r\n", file, line))
}
glog.Error(buffer.String())
httpWriter.WriteHeader(http.StatusInternalServerError)
httpWriter.Write([]byte("recover from panic situation"))
}