-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
76 lines (69 loc) · 1.61 KB
/
error_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package winter
import (
"errors"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)
func TestHalt(t *testing.T) {
var err error
func() {
defer func() {
err = recover().(error)
}()
HaltString(
"test",
HaltWithStatusCode(http.StatusTeapot),
HaltWithExtra("aaa", "bbb"),
HaltWithExtras(map[string]any{
"ccc": "ddd",
"eee": "fff",
}),
)
}()
m := JSONBodyFromError(err)
require.Equal(t, http.StatusTeapot, StatusCodeFromError(err))
require.Equal(t, map[string]any{"message": "test", "aaa": "bbb", "ccc": "ddd", "eee": "fff"}, m)
func() {
defer func() {
err = recover().(error)
}()
HaltString(
"test",
HaltWithBadRequest(),
HaltWithExtras(map[string]any{
"ccc": "ddd",
"eee": "fff",
}),
HaltWithExtra("aaa", "bbb"),
HaltWithMessage("test2"),
)
}()
m = JSONBodyFromError(err)
require.Equal(t, http.StatusBadRequest, StatusCodeFromError(err))
require.Equal(t, map[string]any{"message": "test2", "aaa": "bbb", "ccc": "ddd", "eee": "fff"}, m)
}
func TesPanicError(t *testing.T) {
var err error
func() {
defer func() {
err = recover().(error)
}()
panic(errors.New("TEST1"))
}()
m := JSONBodyFromError(err)
require.Equal(t, http.StatusInternalServerError, StatusCodeFromError(err))
require.Equal(t, map[string]any{"message": "TEST1"}, m)
}
func TesPanicAny(t *testing.T) {
var err error
func() {
defer func() {
err = recover().(error)
}()
panic("TEST1")
}()
m := JSONBodyFromError(err)
require.Equal(t, http.StatusInternalServerError, StatusCodeFromError(err))
require.Equal(t, map[string]any{"message": "panic: TEST1"}, m)
}