Skip to content

Commit

Permalink
Merge pull request #452 from robertopedroso/rescue-with-method
Browse files Browse the repository at this point in the history
Specify rescue handlers and error formatters using a hash
  • Loading branch information
dblock committed Aug 7, 2013
2 parents d5d18d7 + f491d47 commit 723cc3c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
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

0 comments on commit 723cc3c

Please sign in to comment.