From 3ea29982fa1766d56933fc2ddcff7a113f1c9aa1 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 19 Jun 2024 21:03:38 +0200 Subject: [PATCH] Add context in endpoint dsl like Grape::Middleware::Helpers --- lib/grape/dsl/inside_route.rb | 4 ++++ spec/grape/api_spec.rb | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index 8a927550f1..548b813d34 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -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 diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index df8bdbb432..338681363d 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -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