Skip to content

Consolidate rescue handler lookup in Middleware::Error#2688

Merged
dblock merged 1 commit intomasterfrom
refactor/rescue-handler-dedup
Apr 21, 2026
Merged

Consolidate rescue handler lookup in Middleware::Error#2688
dblock merged 1 commit intomasterfrom
refactor/rescue-handler-dedup

Conversation

@ericproulx
Copy link
Copy Markdown
Contributor

Summary

  • Collapse rescue_handler_for_base_only_class and rescue_handler_for_class_or_its_ancestor — which were structurally identical apart from their option key (:base_only_rescue_handlers vs :rescue_handlers) and match predicate (klass == err vs klass <= err) — into a single registered_rescue_handler.
  • Extract the shared lookup scaffolding (options[key]&.find { ... } || method(:default_rescue_handler)) into a rescue_handler_from(key, &matcher) primitive.
  • find_handler now 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 inside registered_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.
  • CI green.

🤖 Generated with Claude Code

`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>
@ericproulx ericproulx force-pushed the refactor/rescue-handler-dedup branch from 1c824d8 to 3b161ad Compare April 20, 2026 22:48
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 20, 2026

Danger Report

No issues found.

View run

@dblock dblock merged commit f4579d5 into master Apr 21, 2026
79 checks passed
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.

2 participants