Remove unnecessary begin..rescue..end, use only rescue

This commit is contained in:
Akira Matsuda 2013-01-05 14:45:18 +09:00
parent 3e8134a750
commit 70ae89c321
15 changed files with 94 additions and 123 deletions

View File

@ -208,11 +208,9 @@ module ActionDispatch
end
def fail_on(exception_class)
begin
yield
rescue exception_class => e
raise MiniTest::Assertion, e.message
end
yield
rescue exception_class => e
raise MiniTest::Assertion, e.message
end
end
end

View File

@ -27,14 +27,12 @@ module ActionView
# new_record: true
# </pre>
def debug(object)
begin
Marshal::dump(object)
object = ERB::Util.html_escape(object.to_yaml).gsub(" ", "&nbsp; ").html_safe
content_tag(:pre, object, :class => "debug_dump")
rescue Exception # errors from Marshal or YAML
# Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback
content_tag(:code, object.to_yaml, :class => "debug_dump")
end
Marshal::dump(object)
object = ERB::Util.html_escape(object.to_yaml).gsub(" ", "&nbsp; ").html_safe
content_tag(:pre, object, :class => "debug_dump")
rescue Exception # errors from Marshal or YAML
# Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback
content_tag(:code, object.to_yaml, :class => "debug_dump")
end
end
end

View File

@ -157,13 +157,11 @@ module AbstractController
private
def self.layout(formats)
find_template(name.underscore, {:formats => formats}, :_prefixes => ["layouts"])
rescue ActionView::MissingTemplate
begin
find_template(name.underscore, {:formats => formats}, :_prefixes => ["layouts"])
find_template("application", {:formats => formats}, :_prefixes => ["layouts"])
rescue ActionView::MissingTemplate
begin
find_template("application", {:formats => formats}, :_prefixes => ["layouts"])
rescue ActionView::MissingTemplate
end
end
end

View File

@ -69,12 +69,10 @@ module AbstractController
end
def test_declare_missing_helper
begin
AbstractHelpers.helper :missing
flunk "should have raised an exception"
rescue LoadError => e
assert_equal "helpers/missing_helper.rb", e.path
end
AbstractHelpers.helper :missing
flunk "should have raised an exception"
rescue LoadError => e
assert_equal "helpers/missing_helper.rb", e.path
end
def test_helpers_with_module_through_block

View File

@ -450,11 +450,9 @@ class FilterTest < ActionController::TestCase
class RescuingAroundFilterWithBlock
def around(controller)
begin
yield
rescue ErrorToRescue => ex
controller.__send__ :render, :text => "I rescued this: #{ex.inspect}"
end
yield
rescue ErrorToRescue => ex
controller.__send__ :render, :text => "I rescued this: #{ex.inspect}"
end
end

View File

@ -793,15 +793,13 @@ class RenderTest < ActionController::TestCase
end
def test_line_offset
begin
get :render_line_offset
flunk "the action should have raised an exception"
rescue StandardError => exc
line = exc.backtrace.first
assert(line =~ %r{:(\d+):})
assert_equal "1", $1,
"The line offset is wrong, perhaps the wrong exception has been raised, exception was: #{exc.inspect}"
end
get :render_line_offset
flunk "the action should have raised an exception"
rescue StandardError => exc
line = exc.backtrace.first
assert(line =~ %r{:(\d+):})
assert_equal "1", $1,
"The line offset is wrong, perhaps the wrong exception has been raised, exception was: #{exc.inspect}"
end
# :ported: compatibility

View File

@ -647,32 +647,30 @@ module ActiveRecord
end
def exec_cache(sql, binds)
begin
stmt_key = prepare_statement sql
stmt_key = prepare_statement sql
# Clear the queue
@connection.get_last_result
@connection.send_query_prepared(stmt_key, binds.map { |col, val|
type_cast(val, col)
})
@connection.block
@connection.get_last_result
rescue PGError => e
# Get the PG code for the failure. Annoyingly, the code for
# prepared statements whose return value may have changed is
# FEATURE_NOT_SUPPORTED. Check here for more details:
# http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
begin
code = e.result.result_error_field(PGresult::PG_DIAG_SQLSTATE)
rescue
raise e
end
if FEATURE_NOT_SUPPORTED == code
@statements.delete sql_key(sql)
retry
else
raise e
end
# Clear the queue
@connection.get_last_result
@connection.send_query_prepared(stmt_key, binds.map { |col, val|
type_cast(val, col)
})
@connection.block
@connection.get_last_result
rescue PGError => e
# Get the PG code for the failure. Annoyingly, the code for
# prepared statements whose return value may have changed is
# FEATURE_NOT_SUPPORTED. Check here for more details:
# http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
begin
code = e.result.result_error_field(PGresult::PG_DIAG_SQLSTATE)
rescue
raise e
end
if FEATURE_NOT_SUPPORTED == code
@statements.delete sql_key(sql)
retry
else
raise e
end
end

View File

@ -49,13 +49,11 @@ module ActiveRecord
end
def test_tables_quoting
begin
@conn.tables(nil, "foo-bar", nil)
flunk
rescue => e
# assertion for *quoted* database properly
assert_match(/database 'foo-bar'/, e.inspect)
end
@conn.tables(nil, "foo-bar", nil)
flunk
rescue => e
# assertion for *quoted* database properly
assert_match(/database 'foo-bar'/, e.inspect)
end
def test_pk_and_sequence_for

View File

@ -37,13 +37,11 @@ module ActiveRecord
end
def test_tables_quoting
begin
@connection.tables(nil, "foo-bar", nil)
flunk
rescue => e
# assertion for *quoted* database properly
assert_match(/database 'foo-bar'/, e.inspect)
end
@connection.tables(nil, "foo-bar", nil)
flunk
rescue => e
# assertion for *quoted* database properly
assert_match(/database 'foo-bar'/, e.inspect)
end
end

View File

@ -300,13 +300,11 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_initialize_with_invalid_attribute
begin
Topic.new({ "title" => "test",
"last_read(1i)" => "2005", "last_read(2i)" => "2", "last_read(3i)" => "31"})
rescue ActiveRecord::MultiparameterAssignmentErrors => ex
assert_equal(1, ex.errors.size)
assert_equal("last_read", ex.errors[0].attribute)
end
Topic.new({ "title" => "test",
"last_read(1i)" => "2005", "last_read(2i)" => "2", "last_read(3i)" => "31"})
rescue ActiveRecord::MultiparameterAssignmentErrors => ex
assert_equal(1, ex.errors.size)
assert_equal("last_read", ex.errors[0].attribute)
end
def test_create_after_initialize_without_block

View File

@ -59,10 +59,9 @@ module Kernel
#
# puts 'This code gets executed and nothing related to ZeroDivisionError was seen'
def suppress(*exception_classes)
begin yield
rescue Exception => e
raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
end
yield
rescue Exception => e
raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
end
# Captures the given stream and returns it:

View File

@ -1,18 +1,16 @@
module Marshal
class << self
def load_with_autoloading(source)
begin
load_without_autoloading(source)
rescue ArgumentError, NameError => exc
if exc.message.match(%r|undefined class/module (.+)|)
# try loading the class/module
$1.constantize
# if it is a IO we need to go back to read the object
source.rewind if source.respond_to?(:rewind)
retry
else
raise exc
end
load_without_autoloading(source)
rescue ArgumentError, NameError => exc
if exc.message.match(%r|undefined class/module (.+)|)
# try loading the class/module
$1.constantize
# if it is a IO we need to go back to read the object
source.rewind if source.respond_to?(:rewind)
retry
else
raise exc
end
end

View File

@ -266,14 +266,12 @@ module ActiveSupport
# 'UnknownModule'.safe_constantize # => nil
# 'UnknownModule::Foo::Bar'.safe_constantize # => nil
def safe_constantize(camel_cased_word)
begin
constantize(camel_cased_word)
rescue NameError => e
raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
e.name.to_s == camel_cased_word.to_s
rescue ArgumentError => e
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
end
constantize(camel_cased_word)
rescue NameError => e
raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
e.name.to_s == camel_cased_word.to_s
rescue ArgumentError => e
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
end
# Returns the suffix that should be added to a number to denote the position

View File

@ -50,14 +50,12 @@ class DurationTest < ActiveSupport::TestCase
end
def test_argument_error
begin
1.second.ago('')
flunk("no exception was raised")
rescue ArgumentError => e
assert_equal 'expected a time or date, got ""', e.message, "ensure ArgumentError is not being raised by dependencies.rb"
rescue Exception => e
flunk("ArgumentError should be raised, but we got #{e.class} instead")
end
1.second.ago('')
flunk("no exception was raised")
rescue ArgumentError => e
assert_equal 'expected a time or date, got ""', e.message, "ensure ArgumentError is not being raised by dependencies.rb"
rescue Exception => e
flunk("ArgumentError should be raised, but we got #{e.class} instead")
end
def test_fractional_weeks

View File

@ -363,12 +363,10 @@ class DependenciesTest < ActiveSupport::TestCase
end
def test_smart_name_error_strings
begin
Object.module_eval "ImaginaryObject"
flunk "No raise!!"
rescue NameError => e
assert e.message.include?("uninitialized constant ImaginaryObject")
end
Object.module_eval "ImaginaryObject"
flunk "No raise!!"
rescue NameError => e
assert e.message.include?("uninitialized constant ImaginaryObject")
end
def test_loadable_constants_for_path_should_handle_empty_autoloads