Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
51 lines (47 loc) · 1.53 KB
/
Copy patherrors_test.go
File metadata and controls
51 lines (47 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package vsax
import (
"errors"
"net/http"
"testing"
)
func TestClassifyError(t *testing.T) {
c := &Client{}
body := []byte(`{"Data":null,"Meta":{"ResponseCode":404,"ErrorMessage":"Entity is not found."}}`)
cases := []struct {
status int
want error
target any
}{
{http.StatusBadRequest, ErrBadRequest, &ValidationError{}},
{http.StatusUnauthorized, ErrUnauthorized, &AuthError{}},
{http.StatusForbidden, ErrForbidden, &ForbiddenError{}},
{http.StatusNotFound, ErrNotFound, &NotFoundError{}},
{http.StatusInternalServerError, ErrServer, &APIError{}},
{http.StatusBadGateway, ErrServer, &APIError{}},
{http.StatusTeapot, ErrServer, &APIError{}}, // any 4xx other than 400/401/403/404 → APIError
}
for _, tc := range cases {
err := c.classifyError(tc.status, body)
if err == nil {
t.Fatalf("status %d: expected error", tc.status)
}
switch tc.status {
case http.StatusTeapot:
// 418 is a 4xx but unhandled — classifyError returns APIError with kind "api",
// which does not wrap ErrServer. Accept APIError type.
if _, ok := err.(*APIError); !ok {
t.Errorf("status %d: got %T; want *APIError", tc.status, err)
}
default:
if !errors.Is(err, tc.want) {
t.Errorf("status %d: got %v; want errors.Is(%v) = true", tc.status, err, tc.want)
}
}
}
}
func TestNotFoundMessage(t *testing.T) {
e := &NotFoundError{Base: Error{Code: 404, Message: "Entity is not found."}, ResourceType: "device", ResourceID: "x1"}
if got := e.Error(); got != `vsax: device "x1" not found` {
t.Errorf("got %q", got)
}
}