1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-16 22:37:07 +00:00

Log panics as error

The defaul http server logs panics to the server's log handler.
That goes to debug level and thus panics are not seen when running
rancher at a normal log level.

This fixes the problem by catching the panic at the top level norman
http handler and logging them as errors there.
This commit is contained in:
Craig Jellick 2019-02-08 13:38:33 -07:00
parent 5078dd2c63
commit b694ecb0eb

View File

@ -2,6 +2,7 @@ package api
import ( import (
"net/http" "net/http"
"runtime/debug"
"sync" "sync"
"github.com/rancher/norman/api/access" "github.com/rancher/norman/api/access"
@ -14,6 +15,7 @@ import (
"github.com/rancher/norman/parse" "github.com/rancher/norman/parse"
"github.com/rancher/norman/store/wrapper" "github.com/rancher/norman/store/wrapper"
"github.com/rancher/norman/types" "github.com/rancher/norman/types"
"github.com/sirupsen/logrus"
) )
type StoreWrapper func(types.Store) types.Store type StoreWrapper func(types.Store) types.Store
@ -168,6 +170,13 @@ func (s *Server) setupDefaults(schema *types.Schema) {
} }
func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
defer func() {
if err := recover(); err != nil && err != http.ErrAbortHandler {
logrus.Error("Panic serving api request: \n" + string(debug.Stack()))
rw.WriteHeader(http.StatusInternalServerError)
}
}()
if apiResponse, err := s.handle(rw, req); err != nil { if apiResponse, err := s.handle(rw, req); err != nil {
s.handleError(apiResponse, err) s.handleError(apiResponse, err)
} }