Gets error code from error as int
This commit is contained in:
parent
f9157dbec1
commit
9a0e9f18c6
@ -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) {
|
||||
|
14
errors.go
14
errors.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user