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

Specify rescue handlers and error formatters using a hash #452

Merged
merged 3 commits into from
Aug 7, 2013
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 @@ -10,6 +10,7 @@ Next Release
* [#448](https://github.com/intridea/grape/pull/448): Adding POST style parameters for DELETE requests - [@dquimper](https://github.com/dquimper).
* [#450](https://github.com/intridea/grape/pull/450): Added option to pass an exception handler lambda as an argument to `rescue_from` - [@robertopedroso](https://github.com/robertopedroso).
* [#443](https://github.com/intridea/grape/pull/443): Let `requires` and `optional` take blocks that initialize new scopes - [@asross](https://github.com/asross).
* [#452](https://github.com/intridea/grape/pull/452): Added `with` as a hash option to specify handlers for `rescue_from` and `error_formatter` [@robertopedroso](https://github.com/robertopedroso).
* Your contribution here.

#### Fixes
Expand Down
17 changes: 14 additions & 3 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,14 @@ def default_error_formatter(new_formatter = nil)
new_formatter ? set(:default_error_formatter, new_formatter) : settings[:default_error_formatter]
end

def error_formatter(format, new_formatter)
settings.imbue(:error_formatters, format.to_sym => new_formatter)
def error_formatter(format, options)
if options.is_a?(Hash) && options.has_key?(:with)
formatter = options[:with]
else
formatter = options
end

settings.imbue(:error_formatters, format.to_sym => formatter)
end

# Specify additional content-types, e.g.:
Expand Down Expand Up @@ -197,13 +203,18 @@ def rescue_from(*args, &block)
handler = block
end

options = args.last.is_a?(Hash) ? args.pop : {}
if options.has_key?(:with)
handler ||= proc { options[:with] }
end

if handler
args.each do |arg|
imbue(:rescue_handlers, { arg => handler })
end
end

imbue(:rescue_options, args.pop) if args.last.is_a?(Hash)
imbue(:rescue_options, options)
set(:rescue_all, true) and return if args.include?(:all)
imbue(:rescued_errors, args)
end
Expand Down
33 changes: 33 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,18 @@ class CommunicationError < RuntimeError; end
end
end

describe '.rescue_from klass, with: method' do
it 'rescues an error with the specified message' do
def rescue_arg_error; Rack::Response.new('rescued with a method', 400); end
subject.rescue_from ArgumentError, with: rescue_arg_error
subject.get('/rescue_method') { raise ArgumentError }

get '/rescue_method'
last_response.status.should == 400
last_response.body.should == 'rescued with a method'
end
end

describe '.error_format' do
it 'rescues all errors and return :txt' do
subject.rescue_from :all
Expand Down Expand Up @@ -1171,6 +1183,27 @@ def self.call(message, backtrace, options, env)
end
end

describe 'with' do
context 'class' do
before :each do
class CustomErrorFormatter
def self.call(message, backtrace, option, env)
"message: #{message} @backtrace"
end
end
end

it 'returns a custom error format' do
subject.rescue_from :all, backtrace: true
subject.error_formatter :txt, with: CustomErrorFormatter
subject.get('/exception') { raise "rain!" }

get '/exception'
last_response.body.should == 'message: rain! @backtrace'
end
end
end

it 'rescues all errors and return :json' do
subject.rescue_from :all
subject.format :json
Expand Down