internal: add Response.Err

Builds a detailed HTTPError + Error if the Response is a failure.
It contains more context than just the HTTPError.
This commit is contained in:
Simon Ser 2022-04-29 16:06:56 +02:00
parent 8cc6542f1c
commit 3f8b212b0d

View File

@ -108,7 +108,7 @@ func (ms *Multistatus) Get(p string) (*Response, error) {
resp := &ms.Responses[i] resp := &ms.Responses[i]
for _, h := range resp.Hrefs { for _, h := range resp.Hrefs {
if path.Clean(h.Path) == p { if path.Clean(h.Path) == p {
return resp, resp.Status.Err() return resp, resp.Err()
} }
} }
} }
@ -156,8 +156,28 @@ func NewErrorResponse(path string, err error) *Response {
} }
} }
func (resp *Response) Err() error {
if resp.Status == nil || resp.Status.Code/100 == 2 {
return nil
}
var err error = resp.Error
if resp.ResponseDescription != "" {
if err != nil {
err = fmt.Errorf("%v (%w)", resp.ResponseDescription, err)
} else {
err = fmt.Errorf("%v", resp.ResponseDescription)
}
}
return &HTTPError{
Code: resp.Status.Code,
Err: err,
}
}
func (resp *Response) Path() (string, error) { func (resp *Response) Path() (string, error) {
err := resp.Status.Err() err := resp.Err()
var path string var path string
if len(resp.Hrefs) == 1 { if len(resp.Hrefs) == 1 {
path = resp.Hrefs[0].Path path = resp.Hrefs[0].Path
@ -174,7 +194,7 @@ func (resp *Response) DecodeProp(values ...interface{}) error {
if err != nil { if err != nil {
return err return err
} }
if err := resp.Status.Err(); err != nil { if err := resp.Err(); err != nil {
return err return err
} }
for _, propstat := range resp.Propstats { for _, propstat := range resp.Propstats {