Skip to content

Commit 21af6be

Browse files
authored
Merge pull request ipfs#152 from ipfs/fix/trailing-error
request: handle trailing errors
2 parents 4d11629 + da05ee4 commit 21af6be

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

request.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package shell
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
89
"io/ioutil"
@@ -41,6 +42,24 @@ func NewRequest(ctx context.Context, url, command string, args ...string) *Reque
4142
}
4243
}
4344

45+
type trailerReader struct {
46+
resp *http.Response
47+
}
48+
49+
func (r *trailerReader) Read(b []byte) (int, error) {
50+
n, err := r.resp.Body.Read(b)
51+
if err != nil {
52+
if e := r.resp.Trailer.Get("X-Stream-Error"); e != "" {
53+
err = errors.New(e)
54+
}
55+
}
56+
return n, err
57+
}
58+
59+
func (r *trailerReader) Close() error {
60+
return r.resp.Body.Close()
61+
}
62+
4463
type Response struct {
4564
Output io.ReadCloser
4665
Error *Error
@@ -109,7 +128,7 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
109128

110129
nresp := new(Response)
111130

112-
nresp.Output = resp.Body
131+
nresp.Output = &trailerReader{resp}
113132
if resp.StatusCode >= http.StatusBadRequest {
114133
e := &Error{
115134
Command: r.Command,

0 commit comments

Comments
 (0)