forked from TrueCloudLab/rclone
rest: Implement IgnoreStatus option to not parse the error return
This commit is contained in:
parent
f046c00d3b
commit
194a8f56e1
1 changed files with 13 additions and 5 deletions
18
rest/rest.go
18
rest/rest.go
|
@ -34,13 +34,18 @@ func NewClient(c *http.Client) *Client {
|
||||||
return api
|
return api
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadBody reads resp.Body into result, closing the body
|
||||||
|
func ReadBody(resp *http.Response) (result []byte, err error) {
|
||||||
|
defer fs.CheckClose(resp.Body, &err)
|
||||||
|
return ioutil.ReadAll(resp.Body)
|
||||||
|
}
|
||||||
|
|
||||||
// defaultErrorHandler doesn't attempt to parse the http body, just
|
// defaultErrorHandler doesn't attempt to parse the http body, just
|
||||||
// returns it in the error message
|
// returns it in the error message
|
||||||
func defaultErrorHandler(resp *http.Response) (err error) {
|
func defaultErrorHandler(resp *http.Response) (err error) {
|
||||||
defer fs.CheckClose(resp.Body, &err)
|
body, err := ReadBody(resp)
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "error reading error out of body")
|
||||||
}
|
}
|
||||||
return errors.Errorf("HTTP error %v (%v) returned body: %q", resp.StatusCode, resp.Status, body)
|
return errors.Errorf("HTTP error %v (%v) returned body: %q", resp.StatusCode, resp.Status, body)
|
||||||
}
|
}
|
||||||
|
@ -84,6 +89,7 @@ type Opts struct {
|
||||||
UserName string // username for Basic Auth
|
UserName string // username for Basic Auth
|
||||||
Password string // password for Basic Auth
|
Password string // password for Basic Auth
|
||||||
Options []fs.OpenOption
|
Options []fs.OpenOption
|
||||||
|
IgnoreStatus bool // if set then we don't check error status or parse error body
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeJSON decodes resp.Body into result
|
// DecodeJSON decodes resp.Body into result
|
||||||
|
@ -179,8 +185,10 @@ func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
if !opts.IgnoreStatus {
|
||||||
return resp, api.errorHandler(resp)
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||||
|
return resp, api.errorHandler(resp)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if opts.NoResponse {
|
if opts.NoResponse {
|
||||||
return resp, resp.Body.Close()
|
return resp, resp.Body.Close()
|
||||||
|
|
Loading…
Add table
Reference in a new issue