cmd/ipfs2: Made '/ipfs' handler return more accurate HTTP response codes, resolves #287
Matt Bell committed
Nov 8, 2014 at 21:41 UTC
35983b480ad73d09101d2a2ba32da0d672e8412f
1 file changed
+10
-1
cmd/ipfs2/ipfsHandler.go
+10
-1
@@ -4,11 +4,13 @@ import (
4
"io"
5
"net/http"
6
7
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
9
10
core "github.com/jbenet/go-ipfs/core"
11
"github.com/jbenet/go-ipfs/importer"
12
dag "github.com/jbenet/go-ipfs/merkledag"
13
+ "github.com/jbenet/go-ipfs/routing"
14
uio "github.com/jbenet/go-ipfs/unixfs/io"
15
u "github.com/jbenet/go-ipfs/util"
16
)
@@ -45,7 +47,14 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
47
48
nd, err := i.ResolvePath(path)
49
if err != nil {
48
- w.WriteHeader(http.StatusInternalServerError)
50
+ if err == routing.ErrNotFound {
51
+ w.WriteHeader(http.StatusNotFound)
52
+ } else if err == context.DeadlineExceeded {
53
+ w.WriteHeader(http.StatusRequestTimeout)
54
+ } else {
55
+ w.WriteHeader(http.StatusBadRequest)
56
+ }
57
+
58
log.Error(err)
59
w.Write([]byte(err.Error()))
60
return