From 3ebb662032ad619ea0028157e3912bbf84b51113 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Sat, 11 Mar 2017 04:25:03 +0100 Subject: [PATCH] gateway: simplify error responses, switch to 404 License: MIT Signed-off-by: Lars Gierth --- core/corehttp/gateway_handler.go | 17 ++++------------- core/corehttp/gateway_test.go | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 67dae923b79..0a958a977ba 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -17,7 +17,6 @@ import ( chunk "github.com/ipfs/go-ipfs/importer/chunk" dag "github.com/ipfs/go-ipfs/merkledag" dagutils "github.com/ipfs/go-ipfs/merkledag/utils" - "github.com/ipfs/go-ipfs/namesys" path "github.com/ipfs/go-ipfs/path" ft "github.com/ipfs/go-ipfs/unixfs" @@ -162,26 +161,18 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr dir := false switch err { case nil: - // core.Resolve worked + // Cat() worked defer dr.Close() case coreiface.ErrIsDir: dir = true - case namesys.ErrResolveFailed: - // Don't log that error as it is just noise - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprintf(w, "Path Resolve error: %s", err.Error()) - log.Info("Path Resolve error: %s", err.Error()) - return case coreiface.ErrOffline: if !i.node.OnlineMode() { - w.WriteHeader(http.StatusServiceUnavailable) - fmt.Fprint(w, "Could not resolve path. Node is in offline mode.") + webError(w, "ipfs cat "+urlPath, err, http.StatusServiceUnavailable) return } fallthrough default: - // all other erros - webError(w, "Path Resolve error", err, http.StatusBadRequest) + webError(w, "ipfs cat "+urlPath, err, http.StatusNotFound) return } @@ -532,7 +523,7 @@ func webErrorWithCode(w http.ResponseWriter, message string, err error, code int w.WriteHeader(code) 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) + fmt.Fprintf(w, "%s: %s\n", message, err) } // return a 500 error and log diff --git a/core/corehttp/gateway_test.go b/core/corehttp/gateway_test.go index a120e57ca69..8f307b2c7e9 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.StatusInternalServerError, "Path Resolve error: " + namesys.ErrResolveFailed.Error()}, + {"localhost:5001", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs cat /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"}, {"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"}, {"example.com", "/", http.StatusOK, "fnord"}, } {