Skip to content

Commit

Permalink
Merge pull request ipfs/go-ipfs-http-client#20 from ipfs/feat/errors
Browse files Browse the repository at this point in the history
add extended error handling

This commit was moved from ipfs/go-ipfs-http-client@55418c1
  • Loading branch information
Stebalien authored Jun 13, 2019
2 parents ee2dec1 + 2612353 commit 2fabc8c
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions client/httpapi/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ipfs/go-ipfs-files"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"

cmds "github.com/ipfs/go-ipfs-cmds"
cmdhttp "github.com/ipfs/go-ipfs-cmds/http"
files "github.com/ipfs/go-ipfs-files"
)

type Error = cmds.Error

type trailerReader struct {
resp *http.Response
}

func (r *trailerReader) Read(b []byte) (int, error) {
n, err := r.resp.Body.Read(b)
if err != nil {
if e := r.resp.Trailer.Get("X-Stream-Error"); e != "" {
if e := r.resp.Trailer.Get(cmdhttp.StreamErrHeader); e != "" {
err = errors.New(e)
}
}
Expand Down Expand Up @@ -74,20 +79,6 @@ func (r *Response) decode(dec interface{}) error {
return err2
}

type Error struct {
Command string
Message string
Code int
}

func (e *Error) Error() string {
var out string
if e.Code != 0 {
out = fmt.Sprintf("%s%d: ", out, e.Code)
}
return out + e.Message
}

func (r *Request) Send(c *http.Client) (*Response, error) {
url := r.getURL()
req, err := http.NewRequest("POST", url, r.Body)
Expand Down Expand Up @@ -121,9 +112,7 @@ func (r *Request) Send(c *http.Client) (*Response, error) {

nresp.Output = &trailerReader{resp}
if resp.StatusCode >= http.StatusBadRequest {
e := &Error{
Command: r.Command,
}
e := new(Error)
switch {
case resp.StatusCode == http.StatusNotFound:
e.Message = "command not found"
Expand All @@ -133,11 +122,23 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! response (%d) read error: %s\n", resp.StatusCode, err)
}
e.Message = string(out)

// set special status codes.
switch resp.StatusCode {
case http.StatusNotFound, http.StatusBadRequest:
e.Code = cmds.ErrClient
case http.StatusTooManyRequests:
e.Code = cmds.ErrRateLimited
case http.StatusForbidden:
e.Code = cmds.ErrForbidden
}
case contentType == "application/json":
if err = json.NewDecoder(resp.Body).Decode(e); err != nil {
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! response (%d) unmarshall error: %s\n", resp.StatusCode, err)
}
default:
// This is a server-side bug (probably).
e.Code = cmds.ErrImplementation
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! unhandled response (%d) encoding: %s", resp.StatusCode, contentType)
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down

0 comments on commit 2fabc8c

Please sign in to comment.