mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
internal: introduce NewErrorResponse
Same as NewOKResponse but for errors.
This commit is contained in:
parent
4e8c5effe3
commit
46ebe58ac2
@ -2,6 +2,7 @@ package internal
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"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) {
|
func (resp *Response) Path() (string, error) {
|
||||||
err := resp.Status.Err()
|
err := resp.Status.Err()
|
||||||
var path string
|
var path string
|
||||||
|
Loading…
Reference in New Issue
Block a user