Replace DAVError with HTTPError + Error

That way we can avoid having different ways of representing the
same error value.
This commit is contained in:
Simon Ser 2022-05-02 11:37:45 +02:00
parent 8738a105fc
commit 4e8c5effe3
4 changed files with 19 additions and 34 deletions

View File

@ -327,14 +327,10 @@ const (
func NewPreconditionError(err PreconditionType) error { func NewPreconditionError(err PreconditionType) error {
name := xml.Name{"urn:ietf:params:xml:ns:caldav", string(err)} name := xml.Name{"urn:ietf:params:xml:ns:caldav", string(err)}
elem := internal.NewRawXMLElement(name, nil, nil) elem := internal.NewRawXMLElement(name, nil, nil)
e := internal.Error{ return &internal.HTTPError{
Raw: []internal.RawXMLValue{ Code: 409,
*elem, Err: &internal.Error{
Raw: []internal.RawXMLValue{*elem},
}, },
} }
return &internal.DAVError{
Code: 409,
Msg: fmt.Sprintf("precondition not met: %s", string(err)),
Err: e,
}
} }

View File

@ -469,14 +469,10 @@ const (
func NewPreconditionError(err PreconditionType) error { func NewPreconditionError(err PreconditionType) error {
name := xml.Name{"urn:ietf:params:xml:ns:carddav", string(err)} name := xml.Name{"urn:ietf:params:xml:ns:carddav", string(err)}
elem := internal.NewRawXMLElement(name, nil, nil) elem := internal.NewRawXMLElement(name, nil, nil)
e := internal.Error{ return &internal.HTTPError{
Raw: []internal.RawXMLValue{ Code: 409,
*elem, Err: &internal.Error{
Raw: []internal.RawXMLValue{*elem},
}, },
} }
return &internal.DAVError{
Code: 409,
Msg: fmt.Sprintf("precondition not met: %s", string(err)),
Err: e,
}
} }

View File

@ -103,14 +103,3 @@ func (err *HTTPError) Error() string {
func (err *HTTPError) Unwrap() error { func (err *HTTPError) Unwrap() error {
return err.Err return err.Err
} }
// DAVError is a XML error with HTTP status and a human readable message
type DAVError struct {
Code int
Msg string
Err Error
}
func (err *DAVError) Error() string {
return err.Msg
}

View File

@ -2,6 +2,7 @@ package internal
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"mime" "mime"
"net/http" "net/http"
@ -10,16 +11,19 @@ import (
) )
func ServeError(w http.ResponseWriter, err error) { func ServeError(w http.ResponseWriter, err error) {
if davErr, ok := err.(*DAVError); ok { code := http.StatusInternalServerError
w.WriteHeader(davErr.Code) var httpErr *HTTPError
ServeXML(w).Encode(davErr.Err) if errors.As(err, &httpErr) {
code = httpErr.Code
}
var errElt *Error
if errors.As(err, &errElt) {
w.WriteHeader(code)
ServeXML(w).Encode(errElt)
return return
} }
code := http.StatusInternalServerError
if httpErr, ok := err.(*HTTPError); ok {
code = httpErr.Code
}
http.Error(w, err.Error(), code) http.Error(w, err.Error(), code)
} }