Skip to content

Commit

Permalink
Add context in endpoint dsl like Grape::Middleware::Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx committed Jun 19, 2024
1 parent f73811a commit 3ea2998
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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

0 comments on commit 3ea2998

Please sign in to comment.