internal: introduce NewErrorResponse

Same as NewOKResponse but for errors.
This commit is contained in:
Simon Ser 2022-04-29 15:13:38 +02:00
parent 4e8c5effe3
commit 46ebe58ac2

View File

@ -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