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

Function TestContextError

context_test.go:1887–1920  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1885}
1886
1887func TestContextError(t *testing.T) {
1888 c, _ := CreateTestContext(httptest.NewRecorder())
1889 assert.Empty(t, c.Errors)
1890
1891 firstErr := errors.New("first error")
1892 c.Error(firstErr) //nolint: errcheck
1893 assert.Len(t, c.Errors, 1)
1894 assert.Equal(t, "Error #01: first error\n", c.Errors.String())
1895
1896 secondErr := errors.New("second error")
1897 c.Error(&Error{ //nolint: errcheck
1898 Err: secondErr,
1899 Meta: "some data 2",
1900 Type: ErrorTypePublic,
1901 })
1902 assert.Len(t, c.Errors, 2)
1903
1904 assert.Equal(t, firstErr, c.Errors[0].Err)
1905 assert.Nil(t, c.Errors[0].Meta)
1906 assert.Equal(t, ErrorTypePrivate, c.Errors[0].Type)
1907
1908 assert.Equal(t, secondErr, c.Errors[1].Err)
1909 assert.Equal(t, "some data 2", c.Errors[1].Meta)
1910 assert.Equal(t, ErrorTypePublic, c.Errors[1].Type)
1911
1912 assert.Equal(t, c.Errors.Last(), c.Errors[1])
1913
1914 defer func() {
1915 if recover() == nil {
1916 t.Error("didn't panic")
1917 }
1918 }()
1919 c.Error(nil) //nolint: errcheck
1920}
1921
1922func TestContextTypedError(t *testing.T) {
1923 c, _ := CreateTestContext(httptest.NewRecorder())

Callers

nothing calls this directly

Calls 4

CreateTestContextFunction · 0.85
StringMethod · 0.65
ErrorMethod · 0.45
LastMethod · 0.45

Tested by

no test coverage detected