@cryptotaxi247 / kubo / commits / bb83ccb15

response: read trailing error headers

This commit was moved from ipfs/go-ipfs-http-client@83dfd84ba8bf2341478bb98d437ae1f14fe29e18

Łukasz Magiera committed Feb 4, 2019 at 19:44 UTC bb83ccb15feb4854c7bdb4821fbcf90b89f36163
1 file changed +20 -4
client/httpapi/response.go
+20 -4
@@ -2,6 +2,7 @@ package httpapi
2
3 import (
4 "encoding/json"
5 + "errors"
6 "fmt"
7 "io"
8 "io/ioutil"
@@ -13,6 +14,24 @@ import (
14 files "github.com/ipfs/go-ipfs-files"
15 )
16
17 +type trailerReader struct {
18 + resp *http.Response
19 +}
20 +
21 +func (r *trailerReader) Read(b []byte) (int, error) {
22 + n, err := r.resp.Body.Read(b)
23 + if err != nil {
24 + if e := r.resp.Trailer.Get("X-Stream-Error"); e != "" {
25 + err = errors.New(e)
26 + }
27 + }
28 + return n, err
29 +}
30 +
31 +func (r *trailerReader) Close() error {
32 + return r.resp.Body.Close()
33 +}
34 +
35 type Response struct {
36 Output io.ReadCloser
37 Error *Error
@@ -56,9 +75,6 @@ type Error struct {
75
76 func (e *Error) Error() string {
77 var out string
59 - if e.Command != "" {
60 - out = e.Command + ": "
61 - }
78 if e.Code != 0 {
79 out = fmt.Sprintf("%s%d: ", out, e.Code)
80 }
@@ -93,7 +109,7 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
109
110 nresp := new(Response)
111
96 - nresp.Output = resp.Body
112 + nresp.Output = &trailerReader{resp}
113 if resp.StatusCode >= http.StatusBadRequest {
114 e := &Error{
115 Command: r.Command,