Skip to content

Commit

Permalink
gateway: simplify error responses, switch to 404
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <[email protected]>
  • Loading branch information
Lars Gierth committed Mar 11, 2017
1 parent eecc766 commit 3ebb662
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
17 changes: 4 additions & 13 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
} {
Expand Down

0 comments on commit 3ebb662

Please sign in to comment.