File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -189,12 +189,19 @@ def default_error_status(new_status = nil)
189189 # @param [Hash] options Options for the rescue usage.
190190 # @option options [Boolean] :backtrace Include a backtrace in the rescue response.
191191 def rescue_from ( *args , &block )
192+ options = args . last . is_a? ( Hash ) ? args . pop : { }
193+
192194 if block_given?
193- args . each do | arg |
194- imbue ( :rescue_handlers , { arg => block } )
195- end
195+ handler = block
196+ elsif options . has_key? ( :with )
197+ handler = proc { options [ :with ] }
196198 end
197- imbue ( :rescue_options , args . pop ) if args . last . is_a? ( Hash )
199+
200+ args . each do |arg |
201+ imbue ( :rescue_handlers , { arg => handler } )
202+ end
203+
204+ imbue ( :rescue_options , options )
198205 set ( :rescue_all , true ) and return if args . include? ( :all )
199206 imbue ( :rescued_errors , args )
200207 end
Original file line number Diff line number Diff line change @@ -975,6 +975,17 @@ def three
975975 lambda { get '/unrescued' } . should raise_error
976976 end
977977
978+ it 'can rescue errors with a method instead of a block' do
979+ def rescue_arg_err ; Rack ::Response . new ( 'Error via method' , 400 ) ; end
980+ subject . rescue_from ArgumentError , with : rescue_arg_err
981+
982+ subject . get ( '/exception' ) { raise ArgumentError }
983+ get '/exception'
984+
985+ last_response . status . should == 400
986+ last_response . body . should == 'Error via method'
987+ end
988+
978989 it 'does not re-raise exceptions of type Grape::Exception::Base' do
979990 class CustomError < Grape ::Exceptions ::Base ; end
980991 subject . get ( '/custom_exception' ) { raise CustomError }
You can’t perform that action at this time.
0 commit comments