-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_response_test.go
More file actions
51 lines (42 loc) · 1.2 KB
/
error_response_test.go
File metadata and controls
51 lines (42 loc) · 1.2 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 restapi
import "testing"
func TestAPI_NewError_KnownCode(t *testing.T) {
t.Parallel()
got := Default().NewError(CodeNotFound)
if got.Code != CodeNotFound || got.Message != "resource not found" {
t.Fatalf("unexpected: %+v", got)
}
if got.Details != nil {
t.Fatalf("Details should be nil by default; got %#v", got.Details)
}
}
func TestAPI_NewError_UnknownCode_FallsBack(t *testing.T) {
t.Parallel()
got := Default().NewError(99999)
if got.Code != CodeInternal {
t.Fatalf("got %+v; want fallback CodeInternal", got)
}
}
func TestAPI_NewError_AppliesOptions_SkipsNil(t *testing.T) {
t.Parallel()
got := Default().NewError(
CodeBadRequest,
nil,
WithMessage("custom"),
WithDetails(map[string]any{"x": 1}),
)
if got.Message != "custom" {
t.Fatalf("WithMessage did not apply; got %q", got.Message)
}
if d, ok := got.Details.(map[string]any); !ok || d["x"] != 1 {
t.Fatalf("WithDetails did not apply; got %#v", got.Details)
}
}
func TestPackage_NewError_DelegatesToDefault(t *testing.T) {
t.Parallel()
a := Default().NewError(CodeBadRequest)
b := NewError(CodeBadRequest)
if a != b {
t.Fatalf("package-level NewError diverged from Default().NewError: %+v vs %+v", a, b)
}
}