Skip to content

Commit 2e0d33b

Browse files
authored
Force endpoint status in error_response and error! (#2530)
* Force endpoint status in error_response and error! Add specs * Add CHANGELOG.md
1 parent 5ce44de commit 2e0d33b

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [#2521](https://github.com/ruby-grape/grape/pull/2521): Fixed typo in README - [@datpmt](https://github.com/datpmt).
2323
* [#2525](https://github.com/ruby-grape/grape/pull/2525): Require logger before active_support - [@ericproulx](https://github.com/ericproulx).
2424
* [#2524](https://github.com/ruby-grape/grape/pull/2524): Fix validators bad encoding - [@ericproulx](https://github.com/ericproulx).
25+
* [#2530](https://github.com/ruby-grape/grape/pull/2530): Fix endpoint's status when rescue_from without a block - [@ericproulx](https://github.com/ericproulx).
2526
* Your contribution here.
2627

2728
### 2.2.0 (2024-09-14)

lib/grape/middleware/error.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def find_handler(klass)
6565

6666
def error_response(error = {})
6767
status = error[:status] || options[:default_status]
68+
env[Grape::Env::API_ENDPOINT].status(status) # error! may not have been called
6869
message = error[:message] || options[:default_message]
6970
headers = { Rack::CONTENT_TYPE => content_type }.tap do |h|
7071
h.merge!(error[:headers]) if error[:headers].is_a?(Hash)
@@ -130,6 +131,7 @@ def run_rescue_handler(handler, error, endpoint)
130131
end
131132

132133
def error!(message, status = options[:default_status], headers = {}, backtrace = [], original_exception = nil)
134+
env[Grape::Env::API_ENDPOINT].status(status) # not error! inside route
133135
rack_response(
134136
status, headers.reverse_merge(Rack::CONTENT_TYPE => content_type),
135137
format_message(message, backtrace, original_exception)

spec/grape/endpoint_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,58 @@ def app
115115
expect(memoized_status).to eq(201)
116116
expect(last_response.body).to eq('Hello')
117117
end
118+
119+
context 'when rescue_from' do
120+
subject { last_request.env[Grape::Env::API_ENDPOINT].status }
121+
122+
before do
123+
post '/'
124+
end
125+
126+
context 'when :all blockless' do
127+
context 'when default_error_status is not set' do
128+
let(:app) do
129+
Class.new(Grape::API) do
130+
rescue_from :all
131+
132+
post { raise StandardError }
133+
end
134+
end
135+
136+
it { is_expected.to eq(last_response.status) }
137+
end
138+
139+
context 'when default_error_status is set' do
140+
let(:app) do
141+
Class.new(Grape::API) do
142+
default_error_status 418
143+
rescue_from :all
144+
145+
post { raise StandardError }
146+
end
147+
end
148+
149+
it { is_expected.to eq(last_response.status) }
150+
end
151+
end
152+
153+
context 'when :with' do
154+
let(:app) do
155+
Class.new(Grape::API) do
156+
helpers do
157+
def handle_argument_error
158+
error!("I'm a teapot!", 418)
159+
end
160+
end
161+
rescue_from ArgumentError, with: :handle_argument_error
162+
163+
post { raise ArgumentError }
164+
end
165+
end
166+
167+
it { is_expected.to eq(last_response.status) }
168+
end
169+
end
118170
end
119171

120172
describe '#header' do

0 commit comments

Comments
 (0)