From 71a2b2f85a76d3e61ee71f757a7dd0a0b11a29d9 Mon Sep 17 00:00:00 2001 From: Raju Ahmed Date: Wed, 21 Jan 2026 08:46:02 +0600 Subject: [PATCH 1/2] feat(context): add GetError and GetErrorSlice methods for error retrieval --- context.go | 10 ++++++++++ context_test.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/context.go b/context.go index e1d9be43..a661e47f 100644 --- a/context.go +++ b/context.go @@ -386,6 +386,11 @@ func (c *Context) GetDuration(key any) time.Duration { return getTyped[time.Duration](c, key) } +// GetError returns the value associated with the key as an error. +func (c *Context) GetError(key any) error { + return getTyped[error](c, key) +} + // GetIntSlice returns the value associated with the key as a slice of integers. func (c *Context) GetIntSlice(key any) []int { return getTyped[[]int](c, key) @@ -451,6 +456,11 @@ func (c *Context) GetStringSlice(key any) []string { return getTyped[[]string](c, key) } +// GetErrorSlice returns the value associated with the key as a slice of errors. +func (c *Context) GetErrorSlice(key any) []error { + return getTyped[[]error](c, key) +} + // GetStringMap returns the value associated with the key as a map of interfaces. func (c *Context) GetStringMap(key any) map[string]any { return getTyped[map[string]any](c, key) diff --git a/context_test.go b/context_test.go index 016fa86d..f44f61a1 100644 --- a/context_test.go +++ b/context_test.go @@ -516,6 +516,14 @@ func TestContextGetDuration(t *testing.T) { assert.Equal(t, time.Second, c.GetDuration("duration")) } +func TestContextGetError(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + key := "error" + value := errors.New("test error") + c.Set(key, value) + assert.Equal(t, value, c.GetError(key)) +} + func TestContextGetIntSlice(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) key := "int-slice" @@ -618,6 +626,14 @@ func TestContextGetStringSlice(t *testing.T) { assert.Equal(t, []string{"foo"}, c.GetStringSlice("slice")) } +func TestContextGetErrorSlice(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + key := "error-slice" + value := []error{errors.New("error1"), errors.New("error2")} + c.Set(key, value) + assert.Equal(t, value, c.GetErrorSlice(key)) +} + func TestContextGetStringMap(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) m := make(map[string]any) From e4f4d5fb922b7a0bc132a456f54ef3e96d12dac7 Mon Sep 17 00:00:00 2001 From: Raju Ahmed Date: Wed, 21 Jan 2026 09:15:32 +0600 Subject: [PATCH 2/2] test(context): add tests for GetError and GetErrorSlice handling missing keys --- context_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/context_test.go b/context_test.go index f44f61a1..a0805e9d 100644 --- a/context_test.go +++ b/context_test.go @@ -522,6 +522,9 @@ func TestContextGetError(t *testing.T) { value := errors.New("test error") c.Set(key, value) assert.Equal(t, value, c.GetError(key)) + + // Test missing key returns nil + assert.Nil(t, c.GetError("missing")) } func TestContextGetIntSlice(t *testing.T) { @@ -632,6 +635,9 @@ func TestContextGetErrorSlice(t *testing.T) { value := []error{errors.New("error1"), errors.New("error2")} c.Set(key, value) assert.Equal(t, value, c.GetErrorSlice(key)) + + // Test missing key returns nil slice + assert.Nil(t, c.GetErrorSlice("missing")) } func TestContextGetStringMap(t *testing.T) {