@cryptotaxi247 / kubo / commits / 2612353d8

use error from go-ipfs-cmds

note: this drops the Command field This commit was moved from ipfs/go-ipfs-http-client@523a26f0a853a2d6779a21d7bad239bf2012833b

Steven Allen committed Jun 13, 2019 at 12:00 UTC 2612353d85385a595e889dfa247363a8a4ab02c3
1 file changed +10 -69
client/httpapi/response.go
+10 -69
@@ -4,56 +4,19 @@ import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 - "github.com/ipfs/go-ipfs-files"
7 "io"
8 "io/ioutil"
9 "mime"
10 "net/http"
11 "net/url"
12 "os"
14 -)
13
16 -// Error codes adapted from go-ipfs-cmds. We should find a better solution.
17 -
18 -// ErrorType signfies a category of errors
19 -type ErrorType uint
20 -
21 -// ErrorTypes convey what category of error ocurred
22 -const (
23 - // ErrNormal is a normal error. The command failed for some reason that's not a bug.
24 - ErrNormal ErrorType = iota
25 - // ErrClient means the client made an invalid request.
26 - ErrClient
27 - // ErrImplementation means there's a bug in the implementation.
28 - ErrImplementation
29 - // ErrRateLimited is returned when the operation has been rate-limited.
30 - ErrRateLimited
31 - // ErrForbidden is returned when the client doesn't have permission to
32 - // perform the requested operation.
33 - ErrForbidden
14 + cmds "github.com/ipfs/go-ipfs-cmds"
15 + cmdhttp "github.com/ipfs/go-ipfs-cmds/http"
16 + files "github.com/ipfs/go-ipfs-files"
17 )
18
36 -func (e ErrorType) Error() string {
37 - return e.String()
38 -}
39 -
40 -func (e ErrorType) String() string {
41 - switch e {
42 - case ErrNormal:
43 - return "command failed"
44 - case ErrClient:
45 - return "invalid argument"
46 - case ErrImplementation:
47 - return "internal error"
48 - case ErrRateLimited:
49 - return "rate limited"
50 - case ErrForbidden:
51 - return "request forbidden"
52 - default:
53 - return "unknown error code"
54 - }
55 -
56 -}
19 +type Error = cmds.Error
20
21 type trailerReader struct {
22 resp *http.Response
@@ -62,7 +25,7 @@ type trailerReader struct {
25 func (r *trailerReader) Read(b []byte) (int, error) {
26 n, err := r.resp.Body.Read(b)
27 if err != nil {
65 - if e := r.resp.Trailer.Get("X-Stream-Error"); e != "" {
28 + if e := r.resp.Trailer.Get(cmdhttp.StreamErrHeader); e != "" {
29 err = errors.New(e)
30 }
31 }
@@ -116,26 +79,6 @@ func (r *Response) decode(dec interface{}) error {
79 return err2
80 }
81
119 -type Error struct {
120 - Command string
121 - Message string
122 - Code ErrorType
123 -}
124 -
125 -// Unwrap returns the base error (an ErrorType). Works with go 1.14 error
126 -// helpers.
127 -func (e *Error) Unwrap() error {
128 - return e.Code
129 -}
130 -
131 -func (e *Error) Error() string {
132 - var out string
133 - if e.Code != 0 {
134 - out = fmt.Sprintf("%s%d: ", out, e.Code)
135 - }
136 - return out + e.Message
137 -}
138 -
82 func (r *Request) Send(c *http.Client) (*Response, error) {
83 url := r.getURL()
84 req, err := http.NewRequest("POST", url, r.Body)
@@ -169,9 +112,7 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
112
113 nresp.Output = &trailerReader{resp}
114 if resp.StatusCode >= http.StatusBadRequest {
172 - e := &Error{
173 - Command: r.Command,
174 - }
115 + e := new(Error)
116 switch {
117 case resp.StatusCode == http.StatusNotFound:
118 e.Message = "command not found"
@@ -185,11 +126,11 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
126 // set special status codes.
127 switch resp.StatusCode {
128 case http.StatusNotFound, http.StatusBadRequest:
188 - e.Code = ErrClient
129 + e.Code = cmds.ErrClient
130 case http.StatusTooManyRequests:
190 - e.Code = ErrRateLimited
131 + e.Code = cmds.ErrRateLimited
132 case http.StatusForbidden:
192 - e.Code = ErrForbidden
133 + e.Code = cmds.ErrForbidden
134 }
135 case contentType == "application/json":
136 if err = json.NewDecoder(resp.Body).Decode(e); err != nil {
@@ -197,7 +138,7 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
138 }
139 default:
140 // This is a server-side bug (probably).
200 - e.Code = ErrImplementation
141 + e.Code = cmds.ErrImplementation
142 fmt.Fprintf(os.Stderr, "ipfs-shell: warning! unhandled response (%d) encoding: %s", resp.StatusCode, contentType)
143 out, err := ioutil.ReadAll(resp.Body)
144 if err != nil {