Pass Grape::Exceptions::ErrorResponse to error_formatter#call#2712
Open
ericproulx wants to merge 1 commit into
Open
Pass Grape::Exceptions::ErrorResponse to error_formatter#call#2712ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
a299398 to
835aed6
Compare
Danger ReportNo issues found. |
835aed6 to
6113acc
Compare
238d183 to
bc9faba
Compare
92dd0aa to
da0cf09
Compare
The error_formatter contract previously took five positional arguments — `(message, backtrace, options, env, original_exception)` — which made it impossible to thread additional context through to custom formatters without breaking every existing override. Switch to keyword arguments and add two fields that have been asked for in #2527: def call(message:, backtrace:, options:, env:, status:, headers:, original_exception:) `status:` makes JSON:API-style error bodies straightforward (the spec embeds the HTTP status code in the response). `headers:` lets formatters react to the response content-type or trace the marker headers set by `error!`. Both were previously only reachable via `env[Grape::Env::API_ENDPOINT].status` and friends. This is a contract break — existing custom formatters that re-declare `call` with the positional signature will need to migrate. See UPGRADING.md for the before/after. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
da0cf09 to
45bd7be
Compare
3 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
Resolves #2527.
Middleware::Error#error_responsealready constructs aGrape::Exceptions::ErrorResponsevalue object — it then unpacks it into five separate positional/keyword arguments to hand to the formatter, which immediately repacks the pieces. Cut out the round-trip: pass the value object through.New formatter signature
error:— a frozenGrape::Exceptions::ErrorResponsecarryingstatus,message,headers,backtrace,original_exception. Built once byerror_response(with defaults fromdefault_status/default_message/ the resolved content-type / fallback backtrace already applied) and threaded through unchanged.env:— Rack env, still needed bypresent(message, env)for entity-presenter resolution.include_backtrace:/include_original_exception:— request-time toggles forwarded from the matchingrescue_fromoptions. Renamed from the oldrescue_options[:backtrace]/[:original_exception]Hash lookups; previously buried insideoptions[:rescue_options].Internal plumbing
Middleware::Error#format_messagenow takes a singleerrorarg and forwards it directly to the formatter. The unsupported-format throw path readserror.backtrace/error.original_exceptionoff the same value object.Middleware::Error#error_responsebuilds the resolvedErrorResponseviaraw.with(status: …, message: …, headers: …, backtrace: …)instead of plucking the five fields into separate locals.Middleware::Error#error!wraps its positional args into anErrorResponsebefore callingformat_message.Side cleanups (carried from earlier on the branch)
:rescue_optionsis now aGrape::DSL::RescueOptionsvalue object (built onData.define(:backtrace, :original_exception)with defaults) instead of a plain Hash.Middleware::Errorexposesinclude_backtrace/include_original_exceptionviaForwardable.def_delegator :rescue_options, :backtrace, :include_backtrace(same fororiginal_exception).DSL::RequestResponse#rescue_fromswitched from**optionsto explicit kwargs (with:,rescue_subclasses: true,backtrace: false,original_exception: false);:rescue_subclassesdefaults totrueso thenil-treated-as-true case-when collapses to a one-liner. The stackable now stores aRescueOptionsinstance.rescue_fromYARD now documentsoriginal_exception:, which has been a real (but undocumented) option since Bubble up to the error_formatter the original exception and the backtrace #1652.Endpoint#build_stackreads the latest stackedRescueOptionsdirectly (namespace_stackable[:rescue_options]&.last) instead of the hash-onlynamespace_stackable_with_hash.options[:default_status]/options[:default_message]lookups inMiddleware::Error#error_responsereplaced with the existingdefault_status/default_messageattr_readers.What this unlocks
JSON:API-style error responses become a one-liner:
Header-aware formatters work the same way:
Breaking change
Custom formatters that override
callwith the old positional signature will break. UPGRADING.md has a before/after section under### Upgrading to >= 3.3with a per-field migration table covering each old positional arg → new accessor (message→error.message,backtrace→error.backtrace,options[:rescue_options][:backtrace]→include_backtrace, etc.).Built-in formatters (
Json,Txt,Xml,SerializableHash) are unaffected — they only overrideformat_structured_message, notcall. Existing tests inspec/grape/api_spec.rb,spec/grape/middleware/exception_spec.rb, andspec/grape/dsl/request_response_spec.rbhave been migrated.Test plan
bundle exec rspec— 2308 examples, 0 failuresspec/grape/api_spec.rb(with status and headers exposed (issue 2527)) verifieserror.statusanderror.headersreach a custom formatter🤖 Generated with Claude Code