From aa73c536909e85e99473f81bb1400b89bec8f933 Mon Sep 17 00:00:00 2001 From: xiekeyang Date: Tue, 2 Jun 2015 10:52:24 +0800 Subject: [PATCH] Panic: Add Handler Signed-off-by: xiekeyang --- cmd/registry/main.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/registry/main.go b/cmd/registry/main.go index df135917..1aba2656 100644 --- a/cmd/registry/main.go +++ b/cmd/registry/main.go @@ -62,6 +62,7 @@ func main() { app := handlers.NewApp(ctx, *config) handler := configureReporting(app) + handler = panicHandler(handler) handler = gorhandlers.CombinedLoggingHandler(os.Stdout, handler) if config.HTTP.Debug.Addr != "" { @@ -273,3 +274,17 @@ func debugServer(addr string) { log.Fatalf("error listening on debug interface: %v", err) } } + +// panicHandler add a HTTP handler to web app. The handler recover the happening +// panic. logrus.Panic transmits panic message to pre-config log hooks, which is +// defined in config.yml. +func panicHandler(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer func() { + if err := recover(); err != nil { + log.Panic(fmt.Sprintf("%v", err)) + } + }() + handler.ServeHTTP(w, r) + }) +}