mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 00:04:19 +00:00
internal: move HTTPError to common file
This is used by both clients and servers now.
This commit is contained in:
parent
a4e0e81003
commit
25df841e2b
@ -3,6 +3,7 @@ package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Depth indicates whether a request applies to the resource's members. It's
|
||||
@ -65,3 +66,36 @@ func FormatOverwrite(overwrite bool) string {
|
||||
return "F"
|
||||
}
|
||||
}
|
||||
|
||||
type HTTPError struct {
|
||||
Code int
|
||||
Err error
|
||||
}
|
||||
|
||||
func HTTPErrorFromError(err error) *HTTPError {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if httpErr, ok := err.(*HTTPError); ok {
|
||||
return httpErr
|
||||
} else {
|
||||
return &HTTPError{http.StatusInternalServerError, err}
|
||||
}
|
||||
}
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
return HTTPErrorFromError(err).Code == http.StatusNotFound
|
||||
}
|
||||
|
||||
func HTTPErrorf(code int, format string, a ...interface{}) *HTTPError {
|
||||
return &HTTPError{code, fmt.Errorf(format, a...)}
|
||||
}
|
||||
|
||||
func (err *HTTPError) Error() string {
|
||||
s := fmt.Sprintf("%v %v", err.Code, http.StatusText(err.Code))
|
||||
if err.Err != nil {
|
||||
return fmt.Sprintf("%v: %v", s, err.Err)
|
||||
} else {
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
@ -9,39 +9,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HTTPError struct {
|
||||
Code int
|
||||
Err error
|
||||
}
|
||||
|
||||
func HTTPErrorFromError(err error) *HTTPError {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if httpErr, ok := err.(*HTTPError); ok {
|
||||
return httpErr
|
||||
} else {
|
||||
return &HTTPError{http.StatusInternalServerError, err}
|
||||
}
|
||||
}
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
return HTTPErrorFromError(err).Code == http.StatusNotFound
|
||||
}
|
||||
|
||||
func HTTPErrorf(code int, format string, a ...interface{}) *HTTPError {
|
||||
return &HTTPError{code, fmt.Errorf(format, a...)}
|
||||
}
|
||||
|
||||
func (err *HTTPError) Error() string {
|
||||
s := fmt.Sprintf("%v %v", err.Code, http.StatusText(err.Code))
|
||||
if err.Err != nil {
|
||||
return fmt.Sprintf("%v: %v", s, err.Err)
|
||||
} else {
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
func ServeError(w http.ResponseWriter, err error) {
|
||||
code := http.StatusInternalServerError
|
||||
if httpErr, ok := err.(*HTTPError); ok {
|
||||
|
Loading…
Reference in New Issue
Block a user