Skip to content

Commit

Permalink
Add context in endpoint dsl (#2453)
Browse files Browse the repository at this point in the history
* Add context in endpoint dsl like Grape::Middleware::Helpers

* Add CHANGELOG
Fix rubocop

* Change to self
  • Loading branch information
ericproulx authored Jun 19, 2024
1 parent 3df163a commit 3a26c2c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#### Fixes

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

### 2.1.0 (2024/06/15)
Expand Down
4 changes: 4 additions & 0 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ def entity_representation_for(entity_class, object, options)
def http_version
env['HTTP_VERSION'] || env[Rack::SERVER_PROTOCOL]
end

def context
self
end
end
end
end
19 changes: 19 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4404,4 +4404,23 @@ def uniqe_id_route
expect(last_response.body).to eq('deprecated')
end
end

context 'rescue_from context' do
subject { last_response }

let(:api) do
Class.new(described_class) do
rescue_from :all do
error!(context.env, 400)
end
get { raise ArgumentError, 'Oops!' }
end
end

let(:app) { api }

before { get '/' }

it { is_expected.to be_bad_request }
end
end

0 comments on commit 3a26c2c

Please sign in to comment.