mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 02:30:39 +08:00
Merge pull request #3777 from ipfs/fix/gateway-status-codes
Return 404 Not Found for failed path resolutions
This commit is contained in:
@ -17,6 +17,7 @@ type ErrorType uint
|
|||||||
const (
|
const (
|
||||||
ErrNormal ErrorType = iota // general errors
|
ErrNormal ErrorType = iota // general errors
|
||||||
ErrClient // error was caused by the client, (e.g. invalid CLI usage)
|
ErrClient // error was caused by the client, (e.g. invalid CLI usage)
|
||||||
|
ErrNotFound // == HTTP 404 Not Found
|
||||||
ErrImplementation // programmer error in the server
|
ErrImplementation // programmer error in the server
|
||||||
// TODO: add more types of errors for better error-specific handling
|
// TODO: add more types of errors for better error-specific handling
|
||||||
)
|
)
|
||||||
|
@ -60,6 +60,10 @@ The resolver can recursively resolve:
|
|||||||
depth = namesys.DefaultDepthLimit
|
depth = namesys.DefaultDepthLimit
|
||||||
}
|
}
|
||||||
output, err := resolver.ResolveN(req.Context(), name, depth)
|
output, err := resolver.ResolveN(req.Context(), name, depth)
|
||||||
|
if err == namesys.ErrResolveFailed {
|
||||||
|
res.SetError(err, cmds.ErrNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
res.SetError(err, cmds.ErrNormal)
|
||||||
return
|
return
|
||||||
|
@ -18,7 +18,6 @@ import (
|
|||||||
chunk "github.com/ipfs/go-ipfs/importer/chunk"
|
chunk "github.com/ipfs/go-ipfs/importer/chunk"
|
||||||
dag "github.com/ipfs/go-ipfs/merkledag"
|
dag "github.com/ipfs/go-ipfs/merkledag"
|
||||||
dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
|
dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
|
||||||
"github.com/ipfs/go-ipfs/namesys"
|
|
||||||
path "github.com/ipfs/go-ipfs/path"
|
path "github.com/ipfs/go-ipfs/path"
|
||||||
ft "github.com/ipfs/go-ipfs/unixfs"
|
ft "github.com/ipfs/go-ipfs/unixfs"
|
||||||
|
|
||||||
@ -169,26 +168,18 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
|
|||||||
dir := false
|
dir := false
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
// core.Resolve worked
|
// Cat() worked
|
||||||
defer dr.Close()
|
defer dr.Close()
|
||||||
case coreiface.ErrIsDir:
|
case coreiface.ErrIsDir:
|
||||||
dir = true
|
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:
|
case coreiface.ErrOffline:
|
||||||
if !i.node.OnlineMode() {
|
if !i.node.OnlineMode() {
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
webError(w, "ipfs cat "+urlPath, err, http.StatusServiceUnavailable)
|
||||||
fmt.Fprint(w, "Could not resolve path. Node is in offline mode.")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fallthrough
|
fallthrough
|
||||||
default:
|
default:
|
||||||
// all other erros
|
webError(w, "ipfs cat "+urlPath, err, http.StatusNotFound)
|
||||||
webError(w, "Path Resolve error", err, http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -532,7 +523,7 @@ func webErrorWithCode(w http.ResponseWriter, message string, err error, code int
|
|||||||
w.WriteHeader(code)
|
w.WriteHeader(code)
|
||||||
|
|
||||||
log.Errorf("%s: %s", message, err) // TODO(cryptix): log 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)
|
fmt.Fprintf(w, "%s: %s\n", message, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a 500 error and log
|
// return a 500 error and log
|
||||||
|
@ -136,7 +136,7 @@ func TestGatewayGet(t *testing.T) {
|
|||||||
{"localhost:5001", "/", http.StatusNotFound, "404 page not found\n"},
|
{"localhost:5001", "/", http.StatusNotFound, "404 page not found\n"},
|
||||||
{"localhost:5001", "/" + k, 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", "/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"},
|
{"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
|
||||||
{"example.com", "/", http.StatusOK, "fnord"},
|
{"example.com", "/", http.StatusOK, "fnord"},
|
||||||
} {
|
} {
|
||||||
|
Reference in New Issue
Block a user