bubble up request errors [#28]
This commit is contained in:
parent
3f8721cd4b
commit
c01be49bed
50
client.go
50
client.go
@ -269,9 +269,12 @@ func (c *Client) RemoveAll(path string) error {
|
||||
}
|
||||
|
||||
// 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)
|
||||
status := c.mkcol(path)
|
||||
status, err := c.mkcol(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if status == 201 {
|
||||
return nil
|
||||
}
|
||||
@ -280,12 +283,16 @@ func (c *Client) Mkdir(path string, _ os.FileMode) error {
|
||||
}
|
||||
|
||||
// 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)
|
||||
status := c.mkcol(path)
|
||||
status, err := c.mkcol(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if status == 201 {
|
||||
return nil
|
||||
} else if status == 409 {
|
||||
}
|
||||
if status == 409 {
|
||||
paths := strings.Split(path, "/")
|
||||
sub := "/"
|
||||
for _, e := range paths {
|
||||
@ -293,7 +300,10 @@ func (c *Client) MkdirAll(path string, _ os.FileMode) error {
|
||||
continue
|
||||
}
|
||||
sub += e + "/"
|
||||
status = c.mkcol(sub)
|
||||
status, err = c.mkcol(sub)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if status != 201 {
|
||||
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
|
||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
|
||||
s := c.put(path, bytes.NewReader(data))
|
||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
|
||||
s, err := c.put(path, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch s {
|
||||
|
||||
case 200, 201, 204:
|
||||
return nil
|
||||
|
||||
case 409:
|
||||
err := c.createParentCollection(path)
|
||||
err = c.createParentCollection(path)
|
||||
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 {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -408,14 +425,17 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return err
|
||||
}
|
||||
|
||||
s := c.put(path, stream)
|
||||
s, err := c.put(path, stream)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch s {
|
||||
case 200, 201, 204:
|
||||
|
42
requests.go
42
requests.go
@ -91,18 +91,19 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
|
||||
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)
|
||||
if err != nil {
|
||||
return 400
|
||||
return
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
if rs.StatusCode == 201 || rs.StatusCode == 405 {
|
||||
return 201
|
||||
status = rs.StatusCode
|
||||
if status == 405 {
|
||||
status = 201
|
||||
}
|
||||
|
||||
return rs.StatusCode
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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) {
|
||||
rq.Header.Add("Destination", Join(c.root, newpath))
|
||||
if overwrite {
|
||||
@ -146,13 +156,18 @@ func (c *Client) doCopyMove(method string, oldpath string, newpath string, overw
|
||||
}
|
||||
})
|
||||
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 {
|
||||
s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
|
||||
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) (err error) {
|
||||
s, data, err := c.doCopyMove(method, oldpath, newpath, overwrite)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if data != nil {
|
||||
defer data.Close()
|
||||
}
|
||||
@ -177,14 +192,15 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
|
||||
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)
|
||||
if err != nil {
|
||||
return 400
|
||||
return
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
return rs.StatusCode
|
||||
status = rs.StatusCode
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) createParentCollection(itemPath string) (err error) {
|
||||
|
Loading…
Reference in New Issue
Block a user