internal: parse WebDAV toplevel <error> elements

This commit is contained in:
Simon Ser 2020-05-13 15:02:52 +02:00
parent f4e3fe8c0a
commit 4c0dc5d900
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 22 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/xml"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"path"
@ -77,8 +78,23 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
return nil, err
}
if resp.StatusCode/100 != 2 {
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
if contentType == "" {
contentType = "text/plain"
}
var wrappedErr error
if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/") {
t, _, _ := mime.ParseMediaType(contentType)
if t == "application/xml" || t == "text/xml" {
var davErr Error
if err := xml.NewDecoder(resp.Body).Decode(&davErr); err != nil {
wrappedErr = err
} else {
wrappedErr = &davErr
}
} else if strings.HasPrefix(t, "text/") {
lr := io.LimitedReader{R: resp.Body, N: 1024}
var buf bytes.Buffer
io.Copy(&buf, &lr)

View File

@ -360,6 +360,11 @@ type Error struct {
Raw []RawXMLValue `xml:",any"`
}
func (err *Error) Error() string {
b, _ := xml.Marshal(err)
return string(b)
}
// https://tools.ietf.org/html/rfc4918#section-15.2
type DisplayName struct {
XMLName xml.Name `xml:"DAV: displayname"`