2022-01-28 17:20:35 +01:00
|
|
|
package gowebdav
|
|
|
|
|
|
|
|
import (
|
2023-02-03 10:18:35 +01:00
|
|
|
"errors"
|
2022-01-28 17:20:35 +01:00
|
|
|
"fmt"
|
2023-11-02 13:10:18 +01:00
|
|
|
"net/http"
|
2022-01-28 17:20:35 +01:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
// ErrAuthChanged must be returned from the Verify method as an error
|
|
|
|
// to trigger a re-authentication / negotiation with a new authenticator.
|
|
|
|
var ErrAuthChanged = errors.New("authentication failed, change algorithm")
|
|
|
|
|
|
|
|
// ErrTooManyRedirects will be used as return error if a request exceeds 10 redirects.
|
|
|
|
var ErrTooManyRedirects = errors.New("stopped after 10 redirects")
|
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
// StatusError implements error and wraps
|
|
|
|
// an erroneous status code.
|
|
|
|
type StatusError struct {
|
|
|
|
Status int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (se StatusError) Error() string {
|
|
|
|
return fmt.Sprintf("%d", se.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrCode returns true if the given error
|
|
|
|
// is an os.PathError wrapping a StatusError
|
|
|
|
// with the given status code.
|
|
|
|
func IsErrCode(err error, code int) bool {
|
|
|
|
if pe, ok := err.(*os.PathError); ok {
|
|
|
|
se, ok := pe.Err.(StatusError)
|
|
|
|
return ok && se.Status == code
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrNotFound is shorthand for IsErrCode
|
|
|
|
// for status 404.
|
|
|
|
func IsErrNotFound(err error) bool {
|
|
|
|
return IsErrCode(err, 404)
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
func NewPathError(op string, path string, statusCode int) error {
|
2022-01-28 17:20:35 +01:00
|
|
|
return &os.PathError{
|
|
|
|
Op: op,
|
|
|
|
Path: path,
|
|
|
|
Err: StatusError{statusCode},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
func NewPathErrorErr(op string, path string, err error) error {
|
2022-01-28 17:20:35 +01:00
|
|
|
return &os.PathError{
|
|
|
|
Op: op,
|
|
|
|
Path: path,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 13:10:18 +01:00
|
|
|
|
|
|
|
// GetStatusCode returns status code of the error
|
|
|
|
func GetStatusCode(err error) int {
|
|
|
|
var pe *os.PathError
|
|
|
|
if !errors.As(err, &pe) {
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
var se StatusError
|
|
|
|
if !errors.As(pe.Err, &se) {
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
return se.Status
|
|
|
|
}
|