Skip to content

Commit

Permalink
Log panics as error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Craig Jellick committed Feb 11, 2019
1 parent 5078dd2 commit b694ecb
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"net/http"
"runtime/debug"
"sync"

"github.com/rancher/norman/api/access"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit b694ecb

Please sign in to comment.