From 82d46a5b5be372a4c0171d41de77992e175c4547 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 15 Jul 2016 12:31:02 +0100 Subject: [PATCH 1/3] gateway: degrade error in gateway to log to reduce noise It logs all errors including expired IPNS keys and other non important errors. License: MIT Signed-off-by: Jakub Sztandera --- core/corehttp/gateway_handler.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 3bfa74b4210..2c6302fb803 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -12,12 +12,12 @@ import ( "time" core "github.com/ipfs/go-ipfs/core" + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" "github.com/ipfs/go-ipfs/importer" chunk "github.com/ipfs/go-ipfs/importer/chunk" dag "github.com/ipfs/go-ipfs/merkledag" dagutils "github.com/ipfs/go-ipfs/merkledag/utils" - - coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" + "github.com/ipfs/go-ipfs/namesys" path "github.com/ipfs/go-ipfs/path" ft "github.com/ipfs/go-ipfs/unixfs" @@ -166,6 +166,11 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr w.WriteHeader(http.StatusServiceUnavailable) fmt.Fprint(w, "Could not resolve path. Node is in offline mode.") return + } else if err == namesys.ErrResolveFailed { + // Don't log that error as it is just noise + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Path Resolve error: %s", err.Error()) + return } else if err != nil { webError(w, "Path Resolve error", err, http.StatusBadRequest) return @@ -531,7 +536,8 @@ func webError(w http.ResponseWriter, message string, err error, defaultCode int) func webErrorWithCode(w http.ResponseWriter, message string, err error, code int) { w.WriteHeader(code) - log.Errorf("%s: %s", message, err) // TODO(cryptix): log errors until we have a better way to expose these (counter metrics maybe) + + log.Errorf("%s: %s", message, err) // TODO(cryptix): log until we have a better way to expose these (counter metrics maybe) fmt.Fprintf(w, "%s: %s", message, err) } From 43320e1b7f91a3af799102dbb0f9bba8cd5de44c Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 8 Sep 2016 11:28:40 +0200 Subject: [PATCH 2/3] gateway: use switch for error handling License: MIT Signed-off-by: Jakub Sztandera --- core/corehttp/gateway_handler.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 2c6302fb803..0e45c198c4e 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -160,22 +160,29 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr dr, err := i.api.Cat(ctx, urlPath) dir := false - if err == coreiface.ErrIsDir { + switch err { + case nil: + // core.Resolve worked + defer dr.Close() + case coreiface.ErrIsDir: dir = true - } else if err == coreiface.ErrOffline { - w.WriteHeader(http.StatusServiceUnavailable) - fmt.Fprint(w, "Could not resolve path. Node is in offline mode.") - return - } else if err == namesys.ErrResolveFailed { + case namesys.ErrResolveFailed: // Don't log that error as it is just noise w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "Path Resolve error: %s", err.Error()) + log.Info("Path Resolve error: %s", err.Error()) return - } else if err != nil { + case coreiface.ErrOffline: + if !i.node.OnlineMode() { + w.WriteHeader(http.StatusServiceUnavailable) + fmt.Fprint(w, "Could not resolve path. Node is in offline mode.") + return + } + fallthrough + default: + // all other erros webError(w, "Path Resolve error", err, http.StatusBadRequest) return - } else { - defer dr.Close() } etag := gopath.Base(urlPath) From ce96b915e75392baaa866b6f23e58f42f862f9d6 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 4 Oct 2016 18:25:23 +0200 Subject: [PATCH 3/3] gateway: change status code of failed namesys resution to 500 License: MIT Signed-off-by: Jakub Sztandera --- core/corehttp/gateway_handler.go | 2 +- core/corehttp/gateway_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 0e45c198c4e..9a665d95ce6 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -168,7 +168,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr dir = true case namesys.ErrResolveFailed: // Don't log that error as it is just noise - w.WriteHeader(http.StatusBadRequest) + w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Path Resolve error: %s", err.Error()) log.Info("Path Resolve error: %s", err.Error()) return diff --git a/core/corehttp/gateway_test.go b/core/corehttp/gateway_test.go index 42e0a6ac948..d4b0e907531 100644 --- a/core/corehttp/gateway_test.go +++ b/core/corehttp/gateway_test.go @@ -136,7 +136,7 @@ func TestGatewayGet(t *testing.T) { {"localhost:5001", "/", http.StatusNotFound, "404 page not found\n"}, {"localhost:5001", "/" + k, http.StatusNotFound, "404 page not found\n"}, {"localhost:5001", "/ipfs/" + k, http.StatusOK, "fnord"}, - {"localhost:5001", "/ipns/nxdomain.example.com", http.StatusBadRequest, "Path Resolve error: " + namesys.ErrResolveFailed.Error()}, + {"localhost:5001", "/ipns/nxdomain.example.com", http.StatusInternalServerError, "Path Resolve error: " + namesys.ErrResolveFailed.Error()}, {"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"}, {"example.com", "/", http.StatusOK, "fnord"}, } {