From 6de76c94b8a78982d364c8efa8e6086384414811 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 22 Jan 2020 13:22:45 +0100 Subject: [PATCH] internal: check for HTTP errors in Client.Do Closes: https://github.com/emersion/go-webdav/issues/19 --- internal/client.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/client.go b/internal/client.go index 950a71e..17f84f0 100644 --- a/internal/client.go +++ b/internal/client.go @@ -67,7 +67,16 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) { if c.username != "" || c.password != "" { req.SetBasicAuth(c.username, c.password) } - return c.http.Do(req) + resp, err := c.http.Do(req) + if err != nil { + return nil, err + } + if resp.StatusCode/100 != 2 { + // TODO: if body is plaintext, read it and populate the error message + resp.Body.Close() + return nil, &HTTPError{Code: resp.StatusCode} + } + return resp, nil } func (c *Client) DoMultiStatus(req *http.Request) (*Multistatus, error) {