diff --git a/internal/elements.go b/internal/elements.go index eb76842..ebabbf7 100644 --- a/internal/elements.go +++ b/internal/elements.go @@ -2,6 +2,7 @@ package internal import ( "encoding/xml" + "errors" "fmt" "net/http" "net/url" @@ -134,6 +135,27 @@ func NewOKResponse(path string) *Response { } } +func NewErrorResponse(path string, err error) *Response { + code := http.StatusInternalServerError + var httpErr *HTTPError + if errors.As(err, &httpErr) { + code = httpErr.Code + } + + var errElt *Error + if !errors.As(err, &errElt) { + errElt = &Error{} + } + + href := Href{Path: path} + return &Response{ + Hrefs: []Href{href}, + Status: &Status{Code: code}, + ResponseDescription: err.Error(), + Error: errElt, + } +} + func (resp *Response) Path() (string, error) { err := resp.Status.Err() var path string