Skip to content

Commit 27ea71c

Browse files
ericproulxclaude
andauthored
Apply a mounting API's default_error_status to routes defined before the mount (#3083)
Each route's settings were forked with `default_error_status ||= 500`, which wrote 500 into the route's own copy whenever nothing above it had set a status yet. An API whose routes were defined before it was mounted, the usual layout, so kept answering 500 from `error!` and from exceptions `rescue_from` answered without a status, even under an API that set `default_error_status`. The 500 now applies where the status is read, so the mounting API's value reaches those routes, as its `format` and `rescue_from` already did, and a mounted API's own value still wins. Replaces #1264. Fixes #1177. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 13b2644 commit 27ea71c

7 files changed

Lines changed: 78 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* [#3079](https://github.com/ruby-grape/grape/pull/3079): Fix two dead links in the README - [@farisbasha](https://github.com/farisbasha).
4545
* [#3080](https://github.com/ruby-grape/grape/pull/3080): Drop the `active_support/isolated_execution_state` require that ActiveSupport now autoloads - [@ericproulx](https://github.com/ericproulx).
4646
* [#3082](https://github.com/ruby-grape/grape/pull/3082): Stop reporting the keys of a param its validators rejected as undeclared - [@ericproulx](https://github.com/ericproulx).
47+
* [#3083](https://github.com/ruby-grape/grape/pull/3083): Apply a mounting API's `default_error_status` to routes defined before the mount - [@ericproulx](https://github.com/ericproulx).
4748
* Your contribution here.
4849

4950
### 4.1.0 (2026-10-31)

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,8 @@ class API < Grape::API
28552855
end
28562856
```
28572857

2858+
It also applies to the APIs it mounts, unless they set their own.
2859+
28582860
### Handling 404
28592861

28602862
For Grape to handle all the 404s for your API, it can be useful to use a catch-all.

‎UPGRADING.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ end
113113

114114
To keep the enclosing handler for such an exception, register its class in the nested scope as well: a scope's class handlers still rank ahead of its own `rescue_from :all`. An exception `rescue_from :all` does not catch, one that is not a `StandardError`, still reaches the enclosing scope's handlers, and so does one raised inside the catch-all's block.
115115

116+
#### A mounting API's `default_error_status` reaches the routes its mounted APIs defined first
117+
118+
An API's `default_error_status` now applies to every route of the APIs it mounts, whichever was defined first ([#3083](https://github.com/ruby-grape/grape/pull/3083)). A route used to keep 500 when nothing above it had set `default_error_status` by the time it was defined. So in the usual layout below, where the mounted API is defined before the API that mounts it, `error!` and the exceptions `rescue_from` answers without a status of their own gave 500:
119+
120+
```ruby
121+
class Status < Grape::API
122+
get :status do
123+
error!('unavailable') # now 400, was 500
124+
end
125+
end
126+
127+
class Base < Grape::API
128+
default_error_status 400
129+
mount Status
130+
end
131+
```
132+
133+
A route defined after the mount already answered 400. A `default_error_status` the mounting API sets after `mount` now applies too, as its `format` and `rescue_from` already did. A mounted API's own `default_error_status` still wins, so to keep 500 in a mounted API, declare `default_error_status 500` in it.
134+
116135
### Upgrading to >= 4.1.0
117136

118137
#### `Rack::Head` wraps the router instead of each endpoint's stack

‎lib/grape/dsl/inside_route.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def configuration
3636
# @param original_exception [Exception] The original exception that caused the error.
3737
# @raise [Grape::Exceptions::Halt] always, carrying the response.
3838
def error!(message, status = nil, additional_headers = nil, backtrace = nil, original_exception = nil)
39-
resolved_status = self.status(status || inheritable_setting.default_error_status)
39+
resolved_status = self.status(status || inheritable_setting.default_error_status || 500)
4040
headers = additional_headers.present? ? header.merge(additional_headers) : header
4141
raise Grape::Exceptions::Halt.new(
4242
message:, status: resolved_status, headers:, backtrace:, original_exception:

‎lib/grape/middleware/error.rb‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@ class Error < Base
1414
) do
1515
def initialize(
1616
all_rescue_handler: nil, base_only_rescue_handlers: nil, content_types: nil,
17-
default_error_formatter: nil, default_message: '', default_status: 500,
17+
default_error_formatter: nil, default_message: '', default_status: nil,
1818
error_formatters: nil, format: :txt,
1919
grape_exceptions_rescue_handler: nil, internal_grape_exceptions_rescue_handler: nil,
2020
rescue_all: false, rescue_grape_exceptions: false, rescue_handlers: nil,
2121
rescue_options: nil
2222
)
23-
# `rescue_options:` and `default_error_formatter:` arrive nil from
24-
# `Endpoint#error_middleware_options` when the API called no
25-
# `rescue_from` and no `format` — and an explicit nil bypasses the
26-
# keyword defaults above, so restore them here rather than letting nil
27-
# reach `rescue_options.backtrace` in `#initialize` or the formatter
28-
# lookup in `#format_message`. A bare `super` hands the parameters
29-
# over as they stand, these two included.
23+
# `rescue_options:`, `default_error_formatter:` and `default_status:`
24+
# arrive nil from `Endpoint#error_middleware_options` when the API
25+
# called no `rescue_from`, no `format` and no `default_error_status` —
26+
# and an explicit nil bypasses a keyword default, so restore them here
27+
# rather than letting nil reach `rescue_options.backtrace` in
28+
# `#initialize`, the formatter lookup in `#format_message` or an error
29+
# response's status. A bare `super` hands the parameters over as they
30+
# stand, these three included.
3031
rescue_options ||= Grape::DSL::RescueOptions.new
3132
default_error_formatter ||= Grape::ErrorFormatter::Txt
33+
default_status ||= 500
3234
super
3335
end
3436
end

‎lib/grape/util/inheritable_setting.rb‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,14 @@ def point_in_time_copy
135135
# Fork a point-in-time copy prepared for a freshly-built endpoint: the
136136
# declared params and validations accumulated by the surrounding scopes
137137
# are snapshotted into the copy's per-route settings, since the
138-
# namespace stacks are wiped between routes (see #reset_validations!),
139-
# and request-serving defaults are applied.
138+
# namespace stacks are wiped between routes (see #reset_validations!).
139+
# No request-serving default is written into the copy: an API that
140+
# mounts this one later still has to reach it (see #inherit_from), so
141+
# the endpoint falls back when it reads one.
140142
def point_in_time_copy_for_endpoint
141143
copy = point_in_time_copy
142144
copy.route_declared_params = copy.declared_params.flatten
143145
copy.route_validations = copy.validations.dup
144-
copy.default_error_status ||= 500
145146
copy
146147
end
147148

‎spec/grape/api_spec.rb‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4043,6 +4043,47 @@ def self.call(object, _env)
40434043
get '/exception'
40444044
expect(last_response).to be_bad_request
40454045
end
4046+
4047+
context 'when set by the API that mounts another' do
4048+
let(:mounted) do
4049+
Class.new(described_class) do
4050+
get('/error') { error! 'rain!' }
4051+
get('/exception') { raise 'rain!' }
4052+
end
4053+
end
4054+
4055+
before { subject.rescue_from :all }
4056+
4057+
it 'applies to error! in the routes the mounted API defined first' do
4058+
subject.default_error_status 400
4059+
subject.mount mounted
4060+
get '/error'
4061+
expect(last_response).to be_bad_request
4062+
end
4063+
4064+
it 'applies to an exception rescued in the routes the mounted API defined first' do
4065+
subject.default_error_status 400
4066+
subject.mount mounted
4067+
get '/exception'
4068+
expect(last_response).to be_bad_request
4069+
end
4070+
4071+
it 'applies when set after the mount, as format and rescue_from do' do
4072+
subject.mount mounted
4073+
subject.default_error_status 400
4074+
get '/error'
4075+
expect(last_response).to be_bad_request
4076+
end
4077+
4078+
it "leaves the mounted API's own default error status" do
4079+
mounted.default_error_status 422
4080+
mounted.get('/own') { error! 'rain!' }
4081+
subject.default_error_status 400
4082+
subject.mount mounted
4083+
get '/own'
4084+
expect(last_response).to be_unprocessable
4085+
end
4086+
end
40464087
end
40474088

40484089
context 'routes' do

0 commit comments

Comments
 (0)