Skip to content

Conversation

@raju-mechatronics
Copy link

Overview

This PR adds two new methods to the Gin Context for type-safe retrieval of error values:

  • GetError(key any) error - retrieves a single error value from context
  • GetErrorSlice(key any) []error - retrieves a slice of errors from context

Motivation

Currently, Gin provides type-safe getters for common types like GetString, GetInt, GetBool, etc., but lacks dedicated methods for retrieving error types from the context. This PR fills that gap by providing consistent, type-safe access to error values stored in the context.

Use Cases

  • Middleware error handling and propagation
  • Collecting validation errors across middleware chain
  • Storing and retrieving domain-specific errors
  • Error aggregation in complex request pipelines

Changes

New Methods

GetError(key any) error

Retrieves an error value from the context. Returns nil if the key doesn't exist or the value is not an error.

func (c *Context) GetError(key any) error {
    return getTyped[error](c, key)
}

GetErrorSlice(key any) []error

Retrieves a slice of errors from the context. Returns nil if the key doesn't exist or the value is not an error slice.

func (c *Context) GetErrorSlice(key any) []error {
    return getTyped[[]error](c, key)
}

Testing

Added TestContextGetError and TestContextGetErrorSlice tests in context_test.go:

All tests pass successfully and maintain consistency with existing context getter test patterns.

Checklist

  • Code follows existing Gin conventions and patterns
  • Uses existing getTyped helper for consistency
  • Comprehensive unit tests added
  • Tests follow existing test patterns in context_test.go
  • Zero-value fallbacks for type safety
  • No breaking changes to existing API
  • Documentation updated in docs/doc.md (if required)

@raju-mechatronics
Copy link
Author

@appleboy do you think it's helpful? I needed this in my project to collect warnings.

@codecov
Copy link

codecov bot commented Jan 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.99%. Comparing base (3dc1cd6) to head (e593061).
⚠️ Report is 232 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4502      +/-   ##
==========================================
- Coverage   99.21%   98.99%   -0.22%     
==========================================
  Files          42       44       +2     
  Lines        3182     2996     -186     
==========================================
- Hits         3157     2966     -191     
- Misses         17       21       +4     
- Partials        8        9       +1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.99% <100.00%> (?)
-tags go_json 98.92% <100.00%> (?)
-tags nomsgpack 98.98% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.24 98.99% <100.00%> (?)
go-1.25 98.99% <100.00%> (?)
macos-latest 98.99% <100.00%> (-0.22%) ⬇️
ubuntu-latest 98.99% <100.00%> (-0.22%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant