Skip to content

Commit 723cc3c

Browse files
committed
Merge pull request #452 from robertopedroso/rescue-with-method
Specify rescue handlers and error formatters using a hash
2 parents d5d18d7 + f491d47 commit 723cc3c

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Next Release
1010
* [#448](https://github.com/intridea/grape/pull/448): Adding POST style parameters for DELETE requests - [@dquimper](https://github.com/dquimper).
1111
* [#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).
1212
* [#443](https://github.com/intridea/grape/pull/443): Let `requires` and `optional` take blocks that initialize new scopes - [@asross](https://github.com/asross).
13+
* [#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).
1314
* Your contribution here.
1415

1516
#### Fixes

lib/grape/api.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,14 @@ def default_error_formatter(new_formatter = nil)
151151
new_formatter ? set(:default_error_formatter, new_formatter) : settings[:default_error_formatter]
152152
end
153153

154-
def error_formatter(format, new_formatter)
155-
settings.imbue(:error_formatters, format.to_sym => new_formatter)
154+
def error_formatter(format, options)
155+
if options.is_a?(Hash) && options.has_key?(:with)
156+
formatter = options[:with]
157+
else
158+
formatter = options
159+
end
160+
161+
settings.imbue(:error_formatters, format.to_sym => formatter)
156162
end
157163

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

206+
options = args.last.is_a?(Hash) ? args.pop : {}
207+
if options.has_key?(:with)
208+
handler ||= proc { options[:with] }
209+
end
210+
200211
if handler
201212
args.each do |arg|
202213
imbue(:rescue_handlers, { arg => handler })
203214
end
204215
end
205216

206-
imbue(:rescue_options, args.pop) if args.last.is_a?(Hash)
217+
imbue(:rescue_options, options)
207218
set(:rescue_all, true) and return if args.include?(:all)
208219
imbue(:rescued_errors, args)
209220
end

spec/grape/api_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,18 @@ class CommunicationError < RuntimeError; end
11061106
end
11071107
end
11081108

1109+
describe '.rescue_from klass, with: method' do
1110+
it 'rescues an error with the specified message' do
1111+
def rescue_arg_error; Rack::Response.new('rescued with a method', 400); end
1112+
subject.rescue_from ArgumentError, with: rescue_arg_error
1113+
subject.get('/rescue_method') { raise ArgumentError }
1114+
1115+
get '/rescue_method'
1116+
last_response.status.should == 400
1117+
last_response.body.should == 'rescued with a method'
1118+
end
1119+
end
1120+
11091121
describe '.error_format' do
11101122
it 'rescues all errors and return :txt' do
11111123
subject.rescue_from :all
@@ -1171,6 +1183,27 @@ def self.call(message, backtrace, options, env)
11711183
end
11721184
end
11731185

1186+
describe 'with' do
1187+
context 'class' do
1188+
before :each do
1189+
class CustomErrorFormatter
1190+
def self.call(message, backtrace, option, env)
1191+
"message: #{message} @backtrace"
1192+
end
1193+
end
1194+
end
1195+
1196+
it 'returns a custom error format' do
1197+
subject.rescue_from :all, backtrace: true
1198+
subject.error_formatter :txt, with: CustomErrorFormatter
1199+
subject.get('/exception') { raise "rain!" }
1200+
1201+
get '/exception'
1202+
last_response.body.should == 'message: rain! @backtrace'
1203+
end
1204+
end
1205+
end
1206+
11741207
it 'rescues all errors and return :json' do
11751208
subject.rescue_from :all
11761209
subject.format :json

0 commit comments

Comments
 (0)