diff --git a/CHANGELOG.md b/CHANGELOG.md index a53c74253..c69e511ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [#1252](https://github.com/ruby-grape/grape/pull/1252): Allow default to be a subset or equal to allowed values without raising IncompatibleOptionValues - [@jeradphelps](https://github.com/jeradphelps). * [#1255](https://github.com/ruby-grape/grape/pull/1255): Allow param type definition in `route_param` - [@namusyaka](https://github.com/namusyaka). * [#1257](https://github.com/ruby-grape/grape/pull/1257): Allow Proc, Symbol or String in `rescue_from with: ...` - [@namusyaka](https://github.com/namusyaka). +* [#1285](https://github.com/ruby-grape/grape/pull/1285): Add a warning for errors appearing in `after` callbacks - [@gregormelhorn](https://github.com/gregormelhorn). * Your contribution here. #### Fixes diff --git a/lib/grape/middleware/base.rb b/lib/grape/middleware/base.rb index c611aa3de..d4f021332 100644 --- a/lib/grape/middleware/base.rb +++ b/lib/grape/middleware/base.rb @@ -29,7 +29,12 @@ def call!(env) begin @app_response = @app.call(@env) ensure - after_response = after + begin + after_response = after + rescue StandardError => e + warn "caught error of type #{e.class} in after callback inside #{self.class.name} : #{e.message}" + raise e + end end response = after_response || @app_response diff --git a/spec/grape/middleware/base_spec.rb b/spec/grape/middleware/base_spec.rb index 9f9137770..6ec58059a 100644 --- a/spec/grape/middleware/base_spec.rb +++ b/spec/grape/middleware/base_spec.rb @@ -48,6 +48,25 @@ end end + context 'after callback with errors' do + it 'does not overwrite the application response' do + expect(subject.call({})).to eq([200, {}, 'Hi there.']) + end + + context 'with patched warnings' do + before do + @warnings = warnings = [] + allow_any_instance_of(Grape::Middleware::Base).to receive(:warn) { |m| warnings << m } + allow(subject).to receive(:after).and_raise(StandardError) + end + + it 'does show a warning' do + expect { subject.call({}) }.to raise_error(StandardError) + expect(@warnings).not_to be_empty + end + end + end + it 'is able to access the response' do subject.call({}) expect(subject.response).to be_kind_of(Rack::Response)