Browse Source

Log panicking HTTP requests

Whilst Go does this by default (does not `exit`), it logs to the std logger
which isn't written to the log file. Handle all panicking `OnIncomingRequests`.
kegan/service-isolation
Kegan Dougal 8 years ago
parent
commit
3efb632b31
  1. 13
      src/github.com/matrix-org/go-neb/server/server.go

13
src/github.com/matrix-org/go-neb/server/server.go

@ -6,6 +6,7 @@ import (
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/matrix-org/go-neb/errors" "github.com/matrix-org/go-neb/errors"
"net/http" "net/http"
"runtime/debug"
) )
// JSONRequestHandler represents an interface that must be satisfied in order to respond to incoming // JSONRequestHandler represents an interface that must be satisfied in order to respond to incoming
@ -41,7 +42,17 @@ func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc {
"method": req.Method, "method": req.Method,
"url": req.URL, "url": req.URL,
}) })
logger.Print(">>> Incoming request")
logger.Print("Incoming request")
defer func() {
if r := recover(); r != nil {
logger.WithField("error", r).Errorf(
"Request panicked!\n%s", debug.Stack(),
)
jsonErrorResponse(
w, req, &errors.HTTPError{nil, "Internal Server Error", 500},
)
}
}()
res, httpErr := handler.OnIncomingRequest(req) res, httpErr := handler.OnIncomingRequest(req)
// Set common headers returned regardless of the outcome of the request // Set common headers returned regardless of the outcome of the request

Loading…
Cancel
Save