mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
internal: read response body on error
This commit is contained in:
parent
a892cc58df
commit
30eac28d2b
@ -77,9 +77,21 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.StatusCode/100 != 2 {
|
if resp.StatusCode/100 != 2 {
|
||||||
|
var wrappedErr error
|
||||||
|
if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/") {
|
||||||
// TODO: if body is plaintext, read it and populate the error message
|
// TODO: if body is plaintext, read it and populate the error message
|
||||||
|
lr := io.LimitedReader{R: resp.Body, N: 1024}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
io.Copy(&buf, &lr)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
return nil, &HTTPError{Code: resp.StatusCode}
|
if s := strings.TrimSpace(buf.String()); s != "" {
|
||||||
|
if lr.N == 0 {
|
||||||
|
s += " […]"
|
||||||
|
}
|
||||||
|
wrappedErr = fmt.Errorf("%v", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, &HTTPError{Code: resp.StatusCode, Err: wrappedErr}
|
||||||
}
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user