From 7455f657408b67390a06e399d38ea26e56ce7ba5 Mon Sep 17 00:00:00 2001 From: Gregor Melhorn Date: Fri, 19 Feb 2016 13:48:13 +0100 Subject: [PATCH] see https://github.com/ruby-grape/grape/issues/1265 --- CHANGELOG.md | 1 + lib/grape/middleware/base.rb | 7 ++++++- spec/grape/middleware/base_spec.rb | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a53c74253f..c69e511cad 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 c611aa3de7..d4f0213329 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 9f9137770e..6ec58059aa 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)