🐛 bug: harden DefaultRes.Format against nil handler panics#4105
🐛 bug: harden DefaultRes.Format against nil handler panics#4105ReneWerner87 merged 1 commit intomainfrom
Conversation
Summary of ChangesHello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
WalkthroughAdds nil Handler validation in DefaultRes.Format to prevent runtime errors, returning a formatted error message when a nil handler is encountered. Corresponding test coverage validates nil handler behavior across first entry, matched media types, and default handler scenarios. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR hardens the DefaultRes.Format method against runtime panics by adding upfront validation for nil handlers in the ResFmt slice. When a user provides a nil handler, the method now returns a descriptive error instead of panicking during content negotiation or handler execution.
Changes:
- Added validation loop in
DefaultRes.Formatto check for nil handlers before content negotiation - Added comprehensive test coverage with three subtests covering different nil handler scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| res.go | Added upfront nil handler validation loop that returns descriptive errors with media type and index information |
| ctx_test.go | Added Test_Ctx_Format_NilHandler with three subtests verifying nil handlers don't panic and return proper errors |
There was a problem hiding this comment.
Code Review
The pull request successfully addresses the potential runtime panics in DefaultRes.Format by adding an upfront validation loop for nil handlers. The implementation is clean, efficient, and includes comprehensive tests covering various edge cases. The defensive check ensures that the API contract is respected and provides helpful debugging information through descriptive error messages.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4105 +/- ##
==========================================
- Coverage 91.03% 91.00% -0.04%
==========================================
Files 119 119
Lines 11324 11340 +16
==========================================
+ Hits 10309 10320 +11
- Misses 643 648 +5
Partials 372 372
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.
| Benchmark suite | Current: 69324b9 | Previous: 92ada8a | Ratio |
|---|---|---|---|
BenchmarkDecoderedirectionMsgs - MB/s |
114.83 MB/s |
47.86 MB/s |
2.40 |
This comment was automatically generated by workflow using github-action-benchmark.
Motivation
ResFmt.Handlerentries are nil by validating handlers before running content negotiation and make debugging easier by returning a descriptive error that includes the media type and handler index.Description
DefaultRes.Formatthat returnsfmt.Errorf("format handler is nil for media type %q at index %d", ...)when anyResFmt.Handleris nil, performed beforeVary/Accept negotiation so existing flow is preserved (matching, default fallback, 406 behavior).StatusNotAcceptablefallback.Test_Ctx_Format_NilHandlerinctx_test.gowith three subtests that assert (and userequire.NotPanics) for: nil handler in the first entry, nil handler in the matched media type, and nil default handler.