@cryptotaxi247 / kubo / commits / 9e4800d40

gateway: simplify error responses, switch to 404

License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>

Lars Gierth committed Mar 11, 2017 at 04:25 UTC 9e4800d40a4ed35e5c7507f3fdb1ec132249b149
2 files changed +5 -14
core/corehttp/gateway_handler.go
+4 -13
@@ -17,7 +17,6 @@ import (
17 chunk "github.com/ipfs/go-ipfs/importer/chunk"
18 dag "github.com/ipfs/go-ipfs/merkledag"
19 dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
20 - "github.com/ipfs/go-ipfs/namesys"
20 path "github.com/ipfs/go-ipfs/path"
21 ft "github.com/ipfs/go-ipfs/unixfs"
22
@@ -162,26 +161,18 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
161 dir := false
162 switch err {
163 case nil:
165 - // core.Resolve worked
164 + // Cat() worked
165 defer dr.Close()
166 case coreiface.ErrIsDir:
167 dir = true
169 - case namesys.ErrResolveFailed:
170 - // Don't log that error as it is just noise
171 - w.WriteHeader(http.StatusInternalServerError)
172 - fmt.Fprintf(w, "Path Resolve error: %s", err.Error())
173 - log.Info("Path Resolve error: %s", err.Error())
174 - return
168 case coreiface.ErrOffline:
169 if !i.node.OnlineMode() {
177 - w.WriteHeader(http.StatusServiceUnavailable)
178 - fmt.Fprint(w, "Could not resolve path. Node is in offline mode.")
170 + webError(w, "ipfs cat "+urlPath, err, http.StatusServiceUnavailable)
171 return
172 }
173 fallthrough
174 default:
183 - // all other erros
184 - webError(w, "Path Resolve error", err, http.StatusBadRequest)
175 + webError(w, "ipfs cat "+urlPath, err, http.StatusNotFound)
176 return
177 }
178
@@ -532,7 +523,7 @@ func webErrorWithCode(w http.ResponseWriter, message string, err error, code int
523 w.WriteHeader(code)
524
525 log.Errorf("%s: %s", message, err) // TODO(cryptix): log until we have a better way to expose these (counter metrics maybe)
535 - fmt.Fprintf(w, "%s: %s", message, err)
526 + fmt.Fprintf(w, "%s: %s\n", message, err)
527 }
528
529 // return a 500 error and log
core/corehttp/gateway_test.go
+1 -1
@@ -136,7 +136,7 @@ func TestGatewayGet(t *testing.T) {
136 {"localhost:5001", "/", http.StatusNotFound, "404 page not found\n"},
137 {"localhost:5001", "/" + k, http.StatusNotFound, "404 page not found\n"},
138 {"localhost:5001", "/ipfs/" + k, http.StatusOK, "fnord"},
139 - {"localhost:5001", "/ipns/nxdomain.example.com", http.StatusInternalServerError, "Path Resolve error: " + namesys.ErrResolveFailed.Error()},
139 + {"localhost:5001", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs cat /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
140 {"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
141 {"example.com", "/", http.StatusOK, "fnord"},
142 } {