Consolidate rescue handler lookup in Middleware::Error#2688
Merged
Conversation
`rescue_handler_for_base_only_class` and `rescue_handler_for_class_or_its_ancestor` were structurally identical — same option-hash lookup, same fallback to `default_rescue_handler`. Only the option key and match predicate differed: `:base_only_rescue_handlers` + `klass == err` for `rescue_from FooError, rescue_subclasses: false`, versus `:rescue_handlers` + `klass <= err` for the default exact-or-ancestor form. Collapse both into `registered_rescue_handler`, which encapsulates the DSL concept (any handler the user registered via `rescue_from`) and preserves exact-before-ancestor precedence. A shared `rescue_handler_from(key, &matcher)` primitive holds the lookup scaffolding once. `find_handler` now reads as a priority list of four named concepts: user- registered handler, Grape exception, catch-all, or re-raise. The exact/ancestor precedence moves one level down, properly encapsulated inside `registered_rescue_handler`. No public API change; behavior is identical. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1c824d8 to
3b161ad
Compare
Danger ReportNo issues found. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rescue_handler_for_base_only_classandrescue_handler_for_class_or_its_ancestor— which were structurally identical apart from their option key (:base_only_rescue_handlersvs:rescue_handlers) and match predicate (klass == errvsklass <= err) — into a singleregistered_rescue_handler.options[key]&.find { ... } || method(:default_rescue_handler)) into arescue_handler_from(key, &matcher)primitive.find_handlernow reads as a priority list of four named concepts: user-registered handler → Grape exception → catch-all → re-raise. Exact-before-ancestor precedence is properly encapsulated insideregistered_rescue_handler.No public API change; behavior is identical.
Test plan
bundle exec rspec spec/grape/middleware/error_spec.rb spec/grape/endpoint_spec.rb spec/grape/api_spec.rb— 551 examples, 0 failures.bundle exec rubocop lib/grape/middleware/error.rb— clean.🤖 Generated with Claude Code