Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if we can decode an error before trying #108

Merged
merged 3 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"sort"
"strings"

"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmdkit/files"
"github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"

osh "github.com/Kubuxu/go-os-helper"
"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmdkit/files"
logging "github.com/ipfs/go-log"
)

var log = logging.Logger("cmds/cli")
Expand Down
2 changes: 2 additions & 0 deletions http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er
}
e.Message = string(mes)
e.Code = cmdkit.ErrNormal
case res.dec == nil:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we also use res.dec == nil to check whether the response is a io.Reader-style byte stream here:

if res.dec == nil {
, doesn't that mean that there are valid cases for the decoder being nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a raw bytes error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean. Can you elaborate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switch is trying to decode an error. Currently, it handles plain-text error responses, and "encoded" error responses (e.g., JSON). I don't think there are any valid cases where res.dec is nil and contentType isn't plain (for errors, at least).

return nil, fmt.Errorf("unknown error content type: %s", contentType)
default:
// handle marshalled errors
err := res.dec.Decode(&e)
Expand Down
10 changes: 10 additions & 0 deletions http/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -197,6 +198,15 @@ func TestParseResponse(t *testing.T) {
},
},
},
{
status: 500,
header: http.Header{
contentTypeHeader: []string{"evil/bad"},
channelHeader: []string{"1"},
},
body: mkbuf("test error"),
err: fmt.Errorf("unknown error content type: %s", "evil/bad"),
},
}

for _, tc := range tcs {
Expand Down