1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-26 23:53:19 +08:00

Merge pull request #1636 from ipfs/fix/panic-ignore

recover and print panics that happen in API server
This commit is contained in:
Juan Benet
2015-09-02 20:50:50 +02:00

View File

@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
@ -103,6 +105,16 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Debug("Incoming API request: ", r.URL)
defer func() {
if r := recover(); r != nil {
log.Error(r)
buf := make([]byte, 4096)
n := runtime.Stack(buf, false)
fmt.Fprintln(os.Stderr, string(buf[:n]))
}
}()
if !allowOrigin(r, i.cfg) || !allowReferer(r, i.cfg) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("403 - Forbidden"))