From 3353277dcc7225b19e16b69c2eafc6af0db3cbce Mon Sep 17 00:00:00 2001 From: Roberto Pedroso Date: Tue, 6 Aug 2013 17:10:20 -0400 Subject: [PATCH 1/3] Added ability to specify a handler method to rescue_from --- lib/grape/api.rb | 7 ++++++- spec/grape/api_spec.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/grape/api.rb b/lib/grape/api.rb index d3170045dd..4afb64df8b 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -197,13 +197,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 diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 83f8f1ca5c..21ae073ca5 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -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 From 3b5fe9373fe28bf9a111af2527eafd7f4cb9fcc4 Mon Sep 17 00:00:00 2001 From: Roberto Pedroso Date: Tue, 6 Aug 2013 18:26:21 -0400 Subject: [PATCH 2/3] Added ability to pass a custom formatter to error_formatter using a hash --- lib/grape/api.rb | 10 ++++++++-- spec/grape/api_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/grape/api.rb b/lib/grape/api.rb index 4afb64df8b..778a5f281b 100644 --- a/lib/grape/api.rb +++ b/lib/grape/api.rb @@ -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.: diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 21ae073ca5..d05a948284 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -1183,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 From f491d4751012fd386ab03a8aa7af61159e9df4ef Mon Sep 17 00:00:00 2001 From: Roberto Pedroso Date: Tue, 6 Aug 2013 18:35:38 -0400 Subject: [PATCH 3/3] Updating changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f922e6ffd4..baad81b5c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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