Skip to content

Commit

Permalink
refactor: apply suggestions from review
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Rataj <[email protected]>
  • Loading branch information
hacdias and lidel committed Feb 23, 2023
1 parent 330d06a commit be6e09c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
15 changes: 9 additions & 6 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,7 @@ func webRequestError(w http.ResponseWriter, err *requestError) {
}

func webError(w http.ResponseWriter, err error, defaultCode int) {
if isErrNoLink(err) {
webErrorWithCode(w, err, http.StatusNotFound)
} else if errors.Is(err, routing.ErrNotFound) || err == routing.ErrNotFound {
webErrorWithCode(w, err, http.StatusNotFound)
} else if ipld.IsNotFound(err) {
if isErrNotFound(err) {
webErrorWithCode(w, err, http.StatusNotFound)
} else if errors.Is(err, ErrGatewayTimeout) {
webErrorWithCode(w, err, http.StatusGatewayTimeout)
Expand All @@ -577,6 +573,13 @@ func webError(w http.ResponseWriter, err error, defaultCode int) {
}
}

func isErrNotFound(err error) bool {
return isErrNoLink(err) ||
errors.Is(err, routing.ErrNotFound) ||
err == routing.ErrNotFound ||
ipld.IsNotFound(err)
}

// isErrNoLink checks if err is a resolver.ErrNoLink. resolver.ErrNoLink
// does not implement a .Is interface and cannot be directly compared to.
// Therefore, errors.Is always returns false with it.
Expand Down Expand Up @@ -939,7 +942,7 @@ func (i *handler) setCommonHeaders(w http.ResponseWriter, r *http.Request, conte
if rootCids, err := i.buildIpfsRootsHeader(contentPath.String(), r); err == nil {
w.Header().Set("X-Ipfs-Roots", rootCids)
} else { // this should never happen, as we resolved the contentPath already
err = fmt.Errorf("error while resolving X-Ipfs-Roots: %s", err)
err = fmt.Errorf("error while resolving X-Ipfs-Roots: %w", err)
return newRequestError(err, http.StatusInternalServerError)
}

Expand Down
2 changes: 1 addition & 1 deletion gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (i *handler) serveCodec(ctx context.Context, w http.ResponseWriter, r *http
toCodec, ok := contentTypeToCodec[requestedContentType]
if !ok {
// This is never supposed to happen unless function is called with wrong parameters.
err := fmt.Errorf("unsupported content type: %s", requestedContentType)
err := fmt.Errorf("unsupported content type: %q", requestedContentType)
webError(w, err, http.StatusInternalServerError)
return false
}
Expand Down
4 changes: 2 additions & 2 deletions gateway/handler_tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (i *handler) serveTAR(ctx context.Context, w http.ResponseWriter, r *http.R
// Get Unixfs file
file, err := i.api.GetUnixFsNode(ctx, resolvedPath)
if err != nil {
err = fmt.Errorf("error getting unixfs node for %s: %w", html.EscapeString(contentPath.String()), err)
webError(w, err, http.StatusBadRequest)
err = fmt.Errorf("error getting UnixFS node for %s: %w", html.EscapeString(contentPath.String()), err)
webError(w, err, http.StatusInternalServerError)
return false
}
defer file.Close()
Expand Down
4 changes: 2 additions & 2 deletions gateway/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestGatewayTimeoutBubblingFromAPI(t *testing.T) {
ts := newTestServer(t, api)
t.Logf("test server url: %s", ts.URL)

req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipns/ipfs.io", nil)
req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipns/en.wikipedia-on-ipfs.org", nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -95,7 +95,7 @@ func TestBadGatewayBubblingFromAPI(t *testing.T) {
ts := newTestServer(t, api)
t.Logf("test server url: %s", ts.URL)

req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipns/ipfs.io", nil)
req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipns/en.wikipedia-on-ipfs.org", nil)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions gateway/handler_unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (i *handler) serveUnixFS(ctx context.Context, w http.ResponseWriter, r *htt
// Handling UnixFS
dr, err := i.api.GetUnixFsNode(ctx, resolvedPath)
if err != nil {
err = fmt.Errorf("error while getting unixfs node: %w", err)
webError(w, err, http.StatusBadRequest)
err = fmt.Errorf("error while getting UnixFS node: %w", err)
webError(w, err, http.StatusInternalServerError)
return false
}
defer dr.Close()
Expand Down

0 comments on commit be6e09c

Please sign in to comment.