Let a parser's Grape errors through the formatter without re-raising - #2932
Closed
ericproulx wants to merge 1 commit into
Closed
Let a parser's Grape errors through the formatter without re-raising#2932ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
`Formatter#read_rack_input` answers a parser's StandardErrors with a 400, and let Grape's own errors go on to the error middleware by rescuing them first, `rescue Grape::Exceptions::Base => e; raise e`. Re-raising an exception makes Ruby read its backtrace, which builds it as Strings, and at request depth that is the dearest part of the error: about 25 µs. It was paid by every malformed body, since the built-in parsers report one as `InvalidMessageBody`. A matcher module in the `rescue` clause now selects the errors to answer here, StandardErrors that are not Grape errors, so a Grape error is never rescued at this frame and travels on as it is. A parser raising something that is not a StandardError, such as an Interrupt or a SystemExit, keeps propagating rather than being answered as a 400. Nothing pinned that; it now has a spec, which passes before and after this change. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
perf/parser-errors-without-reraise
branch
from
September 11, 2026 08:45
11287b5 to
4a5844b
Compare
Danger ReportNo issues found. |
5 tasks
Contributor
Author
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
Formatter#read_rack_inputanswers a parser's StandardErrors with a 400, and let Grape's own errors go on to the error middleware by rescuing them first:rescue Grape::Exceptions::Base => e; raise e. Re-raising an exception makes Ruby read its backtrace (setup_exceptioncallsrb_get_backtrace, which builds the lazily captured backtrace as Strings), and at request depth that is the dearest part of the error: about 25 µs. It was paid by every malformed body, since the built-in parsers report one asInvalidMessageBody.A matcher module in the
rescueclause now selects the errors to answer here, StandardErrors that are not Grape errors, so a Grape error is never rescued at this frame and travels on as it is. Unwinding past a frame that does not rescue it costs nothing extra.Benchmarks
Median of 7 interleaved subprocess rounds, Ruby 4.0.6, no JIT, POST with a malformed JSON body:
rescue_fromrescue_from :allrescue_from :allThe last row is not a contradiction. A backtrace is built once and then cached, so the cost is paid by whichever step reads it first. On an API with
rescue_from :all, master's default handler also reads the backtrace eagerly (fixed by #2931), so either change alone just moves the cost to the other step, and the gain lands once both are in. Withoutrescue_from, nothing else reads it and this change alone gets it all.Missing spec, added
A parser raising something that is not a StandardError, such as an Interrupt or a SystemExit, keeps propagating rather than being answered as a 400. Nothing pinned that: dropping the
StandardErrorbound from the matcher passed the whole suite. The new spec raises aNotImplementedErrorfrom a custom parser; it passes before and after this change.Behaviour
Byte-identical to master over a 30-case matrix of
rescue_from :allAPIs, with and withoutbacktrace: trueandoriginal_exception: true, in JSON, txt and XML, including malformed bodies. The rendered backtraces ofInvalidMessageBodyare identical once paths are normalized, so re-raising never altered them.Test plan
StandardErrorbound fails 1 (the new spec).🤖 Generated with Claude Code