mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add default exceptions affected by suppress (#25099)
* Add default exceptions affected by suppress suppress { do_something_that_might_fail } # instead of begin do_something_that_might_fail rescue end # or do_something_that_might_fail rescue nil * Do not add default exceptions list constant [Rafael Mendonça França + Alexey Zapparov]
This commit is contained in:
parent
d0a23339ac
commit
28492204ee
2 changed files with 10 additions and 0 deletions
|
@ -37,6 +37,7 @@ module Kernel
|
|||
#
|
||||
# puts 'This code gets executed and nothing related to ZeroDivisionError was seen'
|
||||
def suppress(*exception_classes)
|
||||
exception_classes = StandardError if exception_classes.empty?
|
||||
yield
|
||||
rescue *exception_classes
|
||||
end
|
||||
|
|
|
@ -42,6 +42,15 @@ class KernelSuppressTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_suppress_with_defaults
|
||||
suppress { raise RuntimeError }
|
||||
suppress { raise ArgumentError }
|
||||
|
||||
assert_raise(LoadError) do
|
||||
suppress { raise LoadError }
|
||||
end
|
||||
end
|
||||
|
||||
def test_suppression
|
||||
suppress(ArgumentError) { raise ArgumentError }
|
||||
suppress(LoadError) { raise LoadError }
|
||||
|
|
Loading…
Reference in a new issue