mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-04 01:38:12 +08:00
feat(context): add GetError and GetErrorSlice methods for error retrieval
This commit is contained in:
parent
915e4c90d2
commit
e5930610c6
10
context.go
10
context.go
@ -386,6 +386,11 @@ func (c *Context) GetDuration(key any) time.Duration {
|
|||||||
return getTyped[time.Duration](c, key)
|
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.
|
// GetIntSlice returns the value associated with the key as a slice of integers.
|
||||||
func (c *Context) GetIntSlice(key any) []int {
|
func (c *Context) GetIntSlice(key any) []int {
|
||||||
return getTyped[[]int](c, key)
|
return getTyped[[]int](c, key)
|
||||||
@ -451,6 +456,11 @@ func (c *Context) GetStringSlice(key any) []string {
|
|||||||
return getTyped[[]string](c, key)
|
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.
|
// GetStringMap returns the value associated with the key as a map of interfaces.
|
||||||
func (c *Context) GetStringMap(key any) map[string]any {
|
func (c *Context) GetStringMap(key any) map[string]any {
|
||||||
return getTyped[map[string]any](c, key)
|
return getTyped[map[string]any](c, key)
|
||||||
|
|||||||
@ -516,6 +516,14 @@ func TestContextGetDuration(t *testing.T) {
|
|||||||
assert.Equal(t, time.Second, c.GetDuration("duration"))
|
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) {
|
func TestContextGetIntSlice(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
key := "int-slice"
|
key := "int-slice"
|
||||||
@ -618,6 +626,14 @@ func TestContextGetStringSlice(t *testing.T) {
|
|||||||
assert.Equal(t, []string{"foo"}, c.GetStringSlice("slice"))
|
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) {
|
func TestContextGetStringMap(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
m := make(map[string]any)
|
m := make(map[string]any)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user