internal: introduce HrefError
Holds the href value alongside the error. References: https://github.com/emersion/go-webdav/issues/195
This commit is contained in:
parent
71ae569e8a
commit
9f9be606d3
@ -159,10 +159,16 @@ func (resp *Response) Err() error {
|
||||
}
|
||||
}
|
||||
|
||||
return &HTTPError{
|
||||
Code: resp.Status.Code,
|
||||
Err: err,
|
||||
err = &HTTPError{Code: resp.Status.Code, Err: err}
|
||||
if len(resp.Hrefs) == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
hrefErrs := make([]error, len(resp.Hrefs))
|
||||
for i, href := range resp.Hrefs {
|
||||
hrefErrs[i] = &HrefError{Href: url.URL(href), Err: err}
|
||||
}
|
||||
return errors.Join(hrefErrs...)
|
||||
}
|
||||
|
||||
func (resp *Response) Path() (string, error) {
|
||||
|
||||
@ -3,6 +3,7 @@ package internal
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@ -32,9 +33,10 @@ func TestResponse_Err_error(t *testing.T) {
|
||||
resp := ms.Responses[0]
|
||||
|
||||
err := resp.Err()
|
||||
var httpErr *HTTPError
|
||||
if err == nil {
|
||||
t.Errorf("Multistatus.Get() returned a nil error, expected non-nil")
|
||||
} else if httpErr, ok := err.(*HTTPError); !ok {
|
||||
} else if !errors.As(err, &httpErr) {
|
||||
t.Errorf("Multistatus.Get() = %T, expected an *HTTPError", err)
|
||||
} else if httpErr.Code != 423 {
|
||||
t.Errorf("HTTPError.Code = %v, expected 423", httpErr.Code)
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Depth indicates whether a request applies to the resource's members. It's
|
||||
@ -108,3 +109,16 @@ func (err *HTTPError) Error() string {
|
||||
func (err *HTTPError) Unwrap() error {
|
||||
return err.Err
|
||||
}
|
||||
|
||||
type HrefError struct {
|
||||
Href url.URL
|
||||
Err error
|
||||
}
|
||||
|
||||
func (err *HrefError) Error() string {
|
||||
return fmt.Sprintf("%v: %v", err.Href, err.Err)
|
||||
}
|
||||
|
||||
func (err *HrefError) Unwrap() error {
|
||||
return err.Err
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user