Skip to content

Commit 3a26c2c

Browse files
authored
Add context in endpoint dsl (#2453)
* Add context in endpoint dsl like Grape::Middleware::Helpers * Add CHANGELOG Fix rubocop * Change to self
1 parent 3df163a commit 3a26c2c

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#### Fixes
99

10+
* [#2453](https://github.com/ruby-grape/grape/pull/2453): Fix context in rescue_from - [@ericproulx](https://github.com/ericproulx).
1011
* Your contribution here.
1112

1213
### 2.1.0 (2024/06/15)

lib/grape/dsl/inside_route.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,10 @@ def entity_representation_for(entity_class, object, options)
461461
def http_version
462462
env['HTTP_VERSION'] || env[Rack::SERVER_PROTOCOL]
463463
end
464+
465+
def context
466+
self
467+
end
464468
end
465469
end
466470
end

spec/grape/api_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,4 +4404,23 @@ def uniqe_id_route
44044404
expect(last_response.body).to eq('deprecated')
44054405
end
44064406
end
4407+
4408+
context 'rescue_from context' do
4409+
subject { last_response }
4410+
4411+
let(:api) do
4412+
Class.new(described_class) do
4413+
rescue_from :all do
4414+
error!(context.env, 400)
4415+
end
4416+
get { raise ArgumentError, 'Oops!' }
4417+
end
4418+
end
4419+
4420+
let(:app) { api }
4421+
4422+
before { get '/' }
4423+
4424+
it { is_expected.to be_bad_request }
4425+
end
44074426
end

0 commit comments

Comments
 (0)