@cryptotaxi247 / kubo / commits / d85d61c96

recover and print panics that happen in API server

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Sep 2, 2015 at 11:12 UTC d85d61c968b607f5f5bf8682d5211781041a3f28
1 file changed +12
commands/http/handler.go
+12
@@ -7,6 +7,8 @@ import (
7 "io"
8 "net/http"
9 "net/url"
10 + "os"
11 + "runtime"
12 "strconv"
13 "strings"
14
@@ -103,6 +105,16 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
105 func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
106 log.Debug("Incoming API request: ", r.URL)
107
108 + defer func() {
109 + if r := recover(); r != nil {
110 + log.Error(r)
111 +
112 + buf := make([]byte, 4096)
113 + n := runtime.Stack(buf, false)
114 + fmt.Fprintln(os.Stderr, string(buf[:n]))
115 + }
116 + }()
117 +
118 if !allowOrigin(r, i.cfg) || !allowReferer(r, i.cfg) {
119 w.WriteHeader(http.StatusForbidden)
120 w.Write([]byte("403 - Forbidden"))