From b694ecb0eb159776edc3ea84036ee46390c8d7fe Mon Sep 17 00:00:00 2001 From: Craig Jellick Date: Fri, 8 Feb 2019 13:38:33 -0700 Subject: [PATCH] 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. --- api/server.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/api/server.go b/api/server.go index 66fdd8e22..2eb0983f4 100644 --- a/api/server.go +++ b/api/server.go @@ -2,6 +2,7 @@ package api import ( "net/http" + "runtime/debug" "sync" "github.com/rancher/norman/api/access" @@ -14,6 +15,7 @@ import ( "github.com/rancher/norman/parse" "github.com/rancher/norman/store/wrapper" "github.com/rancher/norman/types" + "github.com/sirupsen/logrus" ) 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) { + 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 { s.handleError(apiResponse, err) }