Skip to content
Myr

feat(context): add GetError and GetErrorSlice methods for error retrieval (#4502)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Raju Ahmed · committed by GitHub · parent ac95fa6 · Browse files · 2 files+26 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+26 added−0 removed

  • other:+26 added−0 removed

2 files+26 added−0 removed

modified:context_test.go+16 added
514 unmodified lines
515 c.Set("duration", time.Second)516 assert.Equal(t, time.Second, c.GetDuration("duration"))517}518519func TestContextGetError(t *testing.T) {520 c, _ := CreateTestContext(httptest.NewRecorder())521 key := "error"522 value := errors.New("test error")523 c.Set(key, value)524 assert.Equal(t, value, c.GetError(key))525}526527func TestContextGetIntSlice(t *testing.T) {528 c, _ := CreateTestContext(httptest.NewRecorder())529 key := "int-slice"530 value := []int{1, 2}
94 unmodified lines
625 c.Set("slice", []string{"foo"})626 assert.Equal(t, []string{"foo"}, c.GetStringSlice("slice"))627}628629func TestContextGetErrorSlice(t *testing.T) {630 c, _ := CreateTestContext(httptest.NewRecorder())631 key := "error-slice"632 value := []error{errors.New("error1"), errors.New("error2")}633 c.Set(key, value)634 assert.Equal(t, value, c.GetErrorSlice(key))635}636637func TestContextGetStringMap(t *testing.T) {638 c, _ := CreateTestContext(httptest.NewRecorder())639 m := make(map[string]any)640 m["foo"] = 1
3096 unmodified lines
modified:context.go+10 added
384 unmodified lines
385func (c *Context) GetDuration(key any) time.Duration {386 return getTyped[time.Duration](c, key)387}388389// GetError returns the value associated with the key as an error.390func (c *Context) GetError(key any) error {391 return getTyped[error](c, key)392}393394// GetIntSlice returns the value associated with the key as a slice of integers.395func (c *Context) GetIntSlice(key any) []int {396 return getTyped[[]int](c, key)397}
57 unmodified lines
455func (c *Context) GetStringSlice(key any) []string {456 return getTyped[[]string](c, key)457}458459// GetErrorSlice returns the value associated with the key as a slice of errors.460func (c *Context) GetErrorSlice(key any) []error {461 return getTyped[[]error](c, key)462}463464// GetStringMap returns the value associated with the key as a map of interfaces.465func (c *Context) GetStringMap(key any) map[string]any {466 return getTyped[map[string]any](c, key)467}
1004 unmodified lines