Skip to content

Conversation

@raju-mechatronics
Copy link
Contributor

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
Contributor 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 99.00%. Comparing base (3dc1cd6) to head (37449d1).
⚠️ Report is 236 commits behind head on master.

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

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.

@appleboy appleboy changed the title Add GetError and GetErrorSlice methods to Gin Context chore(context): add GetError and GetErrorSlice methods Jan 24, 2026
@appleboy appleboy added this to the v1.12 milestone Jan 24, 2026
@appleboy
Copy link
Member

@raju-mechatronics LGTM

@appleboy appleboy merged commit 53410d2 into gin-gonic:master Jan 24, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants