Gets error code from error as int

This commit is contained in:
TAV2BJ 2023-11-02 13:10:18 +01:00
parent f9157dbec1
commit 9a0e9f18c6
2 changed files with 18 additions and 0 deletions

View File

@ -327,6 +327,7 @@ func TestReadStreamRangeUnkownLength(t *testing.T) {
func TestStat(t *testing.T) {
cli, srv, _, _ := newServer(t)
defer srv.Close()
wantErrCode := 404
info, err := cli.Stat("/hello.txt")
if err != nil {
@ -343,6 +344,9 @@ func TestStat(t *testing.T) {
if !IsErrNotFound(err) {
t.Fatalf("got: %v, want 404 error", err)
}
if GetStatusCode(err) != wantErrCode {
t.Fatalf("got: %v, want %d", err, wantErrCode)
}
}
func TestMkdir(t *testing.T) {

View File

@ -3,6 +3,7 @@ package gowebdav
import (
"errors"
"fmt"
"net/http"
"os"
)
@ -55,3 +56,16 @@ func NewPathErrorErr(op string, path string, err error) error {
Err: err,
}
}
// 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
}