Switch error_formatter to keyword arguments; expose status and headers#2712
Draft
ericproulx wants to merge 1 commit into
Draft
Switch error_formatter to keyword arguments; expose status and headers#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
6af34ba to
238d183
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>
238d183 to
bc9faba
Compare
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.
The
error_formattercontract previously took five positional arguments and there was no way to expose the HTTPstatus(or the responseheaders) to a custom formatter without a workaround throughenv[Grape::Env::API_ENDPOINT].status. The thread on #2527 (with 👍 from @dblock) converged on switching to keyword arguments so additional context can be threaded through without re-breaking the signature every time.New signature in
Grape::ErrorFormatter::Base:Only
message:is required; everything else has a sensible default (matching the old positional signature for the pre-existing fields andnil/false/{}for the new ones), so future kwargs added on the call site won't break overrides.Two design choices worth calling out:
options:kwarg. The previous positional contract leaked the entire middleware-options hash. The body only ever readoptions.dig(:rescue_options, :backtrace|:original_exception)— both of which are include this in the response body? booleans, not the actual values. Replaced with two flat kwargs (include_backtrace:,include_original_exception:) that read naturally next tobacktrace:(the Array) andoriginal_exception:(the Exception). The other middleware-options keys (default_status,format,rescue_handlers, …) were framework-internal and were never part of the documented contract.status:andheaders:are first-class. Call sites inMiddleware::Error#error_responseand#error!pass the resolved status and the merged response headers (already containing the resolvedContent-Type). Both were previously only reachable viaenv[Grape::Env::API_ENDPOINT].statusand friends.Side cleanups in
Middleware::ErrorandDSL::RequestResponseWhile in the file:
:rescue_optionsis now memoized inMiddleware::Error#initializeand exposed viaattr_readerlike every other option (default_status,default_message, …); the request-time path no longer readsoptions[:rescue_options]andformat_messageno longer needs the per-request|| {}fallback.error_response(options[:default_status],options[:default_message]) replaced with the corresponding attr readers.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 a contract-shaped{ backtrace:, original_exception: }rather than the raw kwargs blob.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.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.3covering both the new kwarg names and the rename fromoptions[:rescue_options][:backtrace]→include_backtrace:(and the equivalent for:original_exception).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— 2293 examples, 0 failuresspec/grape/api_spec.rb(with status and headers exposed (issue 2527)) verifyingstatus:andheaders:reach a custom formatter🤖 Generated with Claude Code