Let rescue_from :grape_exceptions outrank a catch-all class handler - #2843
Open
ericproulx wants to merge 1 commit into
Open
Let rescue_from :grape_exceptions outrank a catch-all class handler#2843ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
ericproulx
force-pushed
the
fix/grape-exceptions-precedence
branch
from
August 1, 2026 10:05
b39fbb4 to
784fa96
Compare
Danger ReportNo issues found. |
rescue_from :grape_exceptions is an opt-in to keep Grape's own errors
rendering with their own status -- a validation failure answers 400 rather
than whatever the application's catch-all returns.
It only ever worked against rescue_from :all, which lives in
all_rescue_handler and is consulted last. Written as a class instead,
rescue_from StandardError is a registered handler, matched first, and Grape's
exceptions are StandardErrors -- so the opt-in silently did nothing and
validation errors still came back as 500s:
rescue_from StandardError { error!('server error', 500) }
rescue_from :grape_exceptions # inert
Let the opt-in win over a handler that only matched through a non-Grape
ancestor. A handler registered for a Grape exception class is more precise
than the opt-in and still wins, so an explicit
rescue_from Grape::Exceptions::ValidationErrors keeps its handler.
registered_rescue_handler gains an _entry variant returning the matched class
alongside its handler, since deciding this needs to know *which* class matched,
not just that one did. find_handler resolves the entry once and passes it down.
Application errors still reach the catch-all, rescue_from :all is unchanged,
and InvalidVersionHeader is left alone so it keeps reaching Rack and version
cascading still works.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
fix/grape-exceptions-precedence
branch
from
August 1, 2026 11:08
784fa96 to
f67e86c
Compare
4 tasks
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_from :grape_exceptionsis an opt-in to keep Grape's own errors rendering with their own status — a validation failure answers400rather than whatever the application's catch-all returns.It only ever worked against
rescue_from :all, which lives inall_rescue_handlerand is consulted last. Written as a class instead,rescue_from StandardErroris a registered handler, matched first, and Grape's exceptions areStandardErrors — so the opt-in silently did nothing:Measured on
GET /againstrequires :n, type: Integer:rescue_from:all:all+:grape_exceptionsStandardErrorStandardError+:grape_exceptions:grape_exceptions+StandardErrorValidationErrors+StandardError+:grape_exceptionsArgumentErrorwith the aboveApproach
Let the opt-in win over a handler that only matched through a non-Grape ancestor. A handler registered for a Grape exception class is more precise than the opt-in and still wins, so an explicit
rescue_from Grape::Exceptions::ValidationErrorskeeps its handler.registered_rescue_handlergains an_entryvariant returning the matched class alongside its handler, since deciding this needs to know which class matched, not just that one did.find_handlerresolves the entry once and passes it down, so there is no extra lookup.Grape::Exceptions::InvalidVersionHeaderis explicitly left alone — it must keep reaching Rack so the next versioned route is tried.Backward compatibility
UPGRADING entry added. An API that registers a catch-all as a class and opts into
:grape_exceptionswill now answer Grape's own status for Grape's own errors, where it previously answered the catch-all's — which is what the opt-in asks for, and makes the two spellings of "catch everything" agree. An API that does not userescue_from :grape_exceptionsis unaffected.Test plan
api_spec.rb: validation stays 400 under a class catch-all, application errors still reach the catch-all, an explicit grape-class handler still wins, and a non-Grape exception is not intercepted. The last three are invariant guards against the change over-reaching; verified the behavioural one fails without thelib/change.🤖 Generated with Claude Code