bubble up request errors [#28]

This commit is contained in:
Ringo Hoffmann 2022-01-22 11:07:58 +01:00
parent 3f8721cd4b
commit c01be49bed
No known key found for this signature in database
GPG Key ID: 5DE1487DA08E34FE
2 changed files with 64 additions and 28 deletions

View File

@ -269,9 +269,12 @@ func (c *Client) RemoveAll(path string) error {
} }
// Mkdir makes a directory // Mkdir makes a directory
func (c *Client) Mkdir(path string, _ os.FileMode) error { func (c *Client) Mkdir(path string, _ os.FileMode) (err error) {
path = FixSlashes(path) path = FixSlashes(path)
status := c.mkcol(path) status, err := c.mkcol(path)
if err != nil {
return
}
if status == 201 { if status == 201 {
return nil return nil
} }
@ -280,12 +283,16 @@ func (c *Client) Mkdir(path string, _ os.FileMode) error {
} }
// MkdirAll like mkdir -p, but for webdav // MkdirAll like mkdir -p, but for webdav
func (c *Client) MkdirAll(path string, _ os.FileMode) error { func (c *Client) MkdirAll(path string, _ os.FileMode) (err error) {
path = FixSlashes(path) path = FixSlashes(path)
status := c.mkcol(path) status, err := c.mkcol(path)
if err != nil {
return
}
if status == 201 { if status == 201 {
return nil return nil
} else if status == 409 { }
if status == 409 {
paths := strings.Split(path, "/") paths := strings.Split(path, "/")
sub := "/" sub := "/"
for _, e := range paths { for _, e := range paths {
@ -293,7 +300,10 @@ func (c *Client) MkdirAll(path string, _ os.FileMode) error {
continue continue
} }
sub += e + "/" sub += e + "/"
status = c.mkcol(sub) status, err = c.mkcol(sub)
if err != nil {
return
}
if status != 201 { if status != 201 {
return newPathError("MkdirAll", sub, status) return newPathError("MkdirAll", sub, status)
} }
@ -385,22 +395,29 @@ func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadClos
} }
// Write writes data to a given path // Write writes data to a given path
func (c *Client) Write(path string, data []byte, _ os.FileMode) error { func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
s := c.put(path, bytes.NewReader(data)) s, err := c.put(path, bytes.NewReader(data))
if err != nil {
return
}
switch s { switch s {
case 200, 201, 204: case 200, 201, 204:
return nil return nil
case 409: case 409:
err := c.createParentCollection(path) err = c.createParentCollection(path)
if err != nil { if err != nil {
return err return
} }
s = c.put(path, bytes.NewReader(data)) s, err = c.put(path, bytes.NewReader(data))
if err != nil {
return
}
if s == 200 || s == 201 || s == 204 { if s == 200 || s == 201 || s == 204 {
return nil return
} }
} }
@ -408,14 +425,17 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
} }
// WriteStream writes a stream // WriteStream writes a stream
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error { func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err error) {
err := c.createParentCollection(path) err = c.createParentCollection(path)
if err != nil { if err != nil {
return err return err
} }
s := c.put(path, stream) s, err := c.put(path, stream)
if err != nil {
return err
}
switch s { switch s {
case 200, 201, 204: case 200, 201, 204:

View File

@ -91,18 +91,19 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
return rs, err return rs, err
} }
func (c *Client) mkcol(path string) int { func (c *Client) mkcol(path string) (status int, err error) {
rs, err := c.req("MKCOL", path, nil, nil) rs, err := c.req("MKCOL", path, nil, nil)
if err != nil { if err != nil {
return 400 return
} }
defer rs.Body.Close() defer rs.Body.Close()
if rs.StatusCode == 201 || rs.StatusCode == 405 { status = rs.StatusCode
return 201 if status == 405 {
status = 201
} }
return rs.StatusCode return
} }
func (c *Client) options(path string) (*http.Response, error) { func (c *Client) options(path string) (*http.Response, error) {
@ -136,7 +137,16 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
return parseXML(rs.Body, resp, parse) return parseXML(rs.Body, resp, parse)
} }
func (c *Client) doCopyMove(method string, oldpath string, newpath string, overwrite bool) (int, io.ReadCloser) { func (c *Client) doCopyMove(
method string,
oldpath string,
newpath string,
overwrite bool,
) (
status int,
r io.ReadCloser,
err error,
) {
rs, err := c.req(method, oldpath, nil, func(rq *http.Request) { rs, err := c.req(method, oldpath, nil, func(rq *http.Request) {
rq.Header.Add("Destination", Join(c.root, newpath)) rq.Header.Add("Destination", Join(c.root, newpath))
if overwrite { if overwrite {
@ -146,13 +156,18 @@ func (c *Client) doCopyMove(method string, oldpath string, newpath string, overw
} }
}) })
if err != nil { if err != nil {
return 400, nil return
} }
return rs.StatusCode, rs.Body status = rs.StatusCode
r = rs.Body
return
} }
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error { func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) (err error) {
s, data := c.doCopyMove(method, oldpath, newpath, overwrite) s, data, err := c.doCopyMove(method, oldpath, newpath, overwrite)
if err != nil {
return
}
if data != nil { if data != nil {
defer data.Close() defer data.Close()
} }
@ -177,14 +192,15 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
return newPathError(method, oldpath, s) return newPathError(method, oldpath, s)
} }
func (c *Client) put(path string, stream io.Reader) int { func (c *Client) put(path string, stream io.Reader) (status int, err error) {
rs, err := c.req("PUT", path, stream, nil) rs, err := c.req("PUT", path, stream, nil)
if err != nil { if err != nil {
return 400 return
} }
defer rs.Body.Close() defer rs.Body.Close()
return rs.StatusCode status = rs.StatusCode
return
} }
func (c *Client) createParentCollection(itemPath string) (err error) { func (c *Client) createParentCollection(itemPath string) (err error) {