Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After callback warning #1285

Merged
merged 1 commit into from
Feb 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/grape/middleware/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change this, but it reads more English to say to_not :)

end
end
end

it 'is able to access the response' do
subject.call({})
expect(subject.response).to be_kind_of(Rack::Response)
Expand Down