diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 3bfa74b42..9a665d95c 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" @@ -160,17 +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.") + 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 - } 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) @@ -531,7 +543,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) } diff --git a/core/corehttp/gateway_test.go b/core/corehttp/gateway_test.go index 42e0a6ac9..d4b0e9075 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"}, } {