Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context in endpoint dsl #2453

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add context in endpoint dsl like Grape::Middleware::Helpers
  • Loading branch information
ericproulx committed Jun 19, 2024
commit 3ea29982fa1766d56933fc2ddcff7a113f1c9aa1
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
env[Grape::Env::API_ENDPOINT]
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
Loading