From 9e4800d40a4ed35e5c7507f3fdb1ec132249b149 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Sat, 11 Mar 2017 04:25:03 +0100 Subject: [PATCH 1/2] 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 67dae923b..0a958a977 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 a120e57ca..8f307b2c7 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"}, } { From 807ffb9141909a081c651c8cf519d0ae50401971 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Sat, 11 Mar 2017 04:48:24 +0100 Subject: [PATCH 2/2] commands/dns: return NotFound error for failed resolutions License: MIT Signed-off-by: Lars Gierth --- commands/response.go | 1 + core/commands/dns.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/commands/response.go b/commands/response.go index 913b684bd..e843f9a0c 100644 --- a/commands/response.go +++ b/commands/response.go @@ -17,6 +17,7 @@ type ErrorType uint const ( ErrNormal ErrorType = iota // general errors ErrClient // error was caused by the client, (e.g. invalid CLI usage) + ErrNotFound // == HTTP 404 Not Found ErrImplementation // programmer error in the server // TODO: add more types of errors for better error-specific handling ) diff --git a/core/commands/dns.go b/core/commands/dns.go index 90ea36786..099ad6e02 100644 --- a/core/commands/dns.go +++ b/core/commands/dns.go @@ -60,6 +60,10 @@ The resolver can recursively resolve: depth = namesys.DefaultDepthLimit } output, err := resolver.ResolveN(req.Context(), name, depth) + if err == namesys.ErrResolveFailed { + res.SetError(err, cmds.ErrNotFound) + return + } if err != nil { res.SetError(err, cmds.ErrNormal) return