From 334aabba8fc487bcbce60284a6e2502833cbb102 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 1 Sep 2024 13:44:19 +0200 Subject: [PATCH] Move inspect method to public Calls super if env is not defined Add spec --- lib/grape/endpoint.rb | 13 +++++++++---- spec/grape/endpoint_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 88605d13b8..e805a07244 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -234,6 +234,15 @@ def equals?(endpoint) (options == endpoint.options) && (inheritable_setting.to_hash == endpoint.inheritable_setting.to_hash) end + # the purpose of this override is solely for stripping internals when an error occurs while calling + # an endpoint through an api. See #https://github.com/ruby-grape/grape/issues/2398 + # Otherwise, it calls super + def inspect + return super unless env + + "#{self.class} in `#{route.origin}' endpoint" + end + protected def run @@ -403,9 +412,5 @@ def options? options[:options_route_enabled] && env[Rack::REQUEST_METHOD] == Rack::OPTIONS end - - def inspect - "#{self.class} in `#{route.origin}' endpoint" - end end end diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index ee44efc39a..009c67ae7c 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -1088,4 +1088,24 @@ def memoized ) end end + + context '#inspect' do + subject { described_class.new(settings, options).inspect } + + let(:options) do + { + method: :path, + path: '/path', + app: {}, + route_options: { anchor: false }, + forward_match: true, + for: Class.new + } + end + let(:settings) { Grape::Util::InheritableSetting.new } + + it 'does not raise an error' do + expect { subject }.to_not raise_error + end + end end