MCPcopy
hub / github.com/gin-gonic/gin / TestErrorSlice

Function TestErrorSlice

errors_test.go:68–108  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

66}
67
68func TestErrorSlice(t *testing.T) {
69 errs := errorMsgs{
70 {Err: errors.New("first"), Type: ErrorTypePrivate},
71 {Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},
72 {Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},
73 }
74
75 assert.Equal(t, errs, errs.ByType(ErrorTypeAny))
76 assert.Equal(t, "third", errs.Last().Error())
77 assert.Equal(t, []string{"first", "second", "third"}, errs.Errors())
78 assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())
79 assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())
80 assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())
81 assert.Empty(t, errs.ByType(ErrorTypeBind))
82 assert.Empty(t, errs.ByType(ErrorTypeBind).String())
83
84 assert.Equal(t, `Error #01: first
85Error #02: second
86 Meta: some data
87Error #03: third
88 Meta: map[status:400]
89`, errs.String())
90 assert.Equal(t, []any{
91 H{"error": "first"},
92 H{"error": "second", "meta": "some data"},
93 H{"error": "third", "status": "400"},
94 }, errs.JSON())
95 jsonBytes, _ := json.API.Marshal(errs)
96 assert.JSONEq(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
97 errs = errorMsgs{
98 {Err: errors.New("first"), Type: ErrorTypePrivate},
99 }
100 assert.Equal(t, H{"error": "first"}, errs.JSON())
101 jsonBytes, _ = json.API.Marshal(errs)
102 assert.JSONEq(t, "{\"error\":\"first\"}", string(jsonBytes))
103
104 errs = errorMsgs{}
105 assert.Nil(t, errs.Last())
106 assert.Nil(t, errs.JSON())
107 assert.Empty(t, errs.String())
108}
109
110type TestErr string
111

Callers

nothing calls this directly

Calls 8

ByTypeMethod · 0.95
LastMethod · 0.95
ErrorsMethod · 0.95
StringMethod · 0.95
JSONMethod · 0.95
StringMethod · 0.65
MarshalMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…