From ab59522ea1ded4dcf8fd6c6f94bbc7385e50c298 Mon Sep 17 00:00:00 2001 From: Thiago Pradi Date: Sat, 11 Sep 2010 11:00:37 -0300 Subject: [PATCH] Exception handling more readable [#5601 state:committed] Signed-off-by: Santiago Pastorino --- actionpack/test/controller/assert_select_test.rb | 16 ++++++++-------- .../abstract/connection_specification.rb | 4 ++-- .../lib/active_record/railties/databases.rake | 8 ++++---- activeresource/lib/active_resource/base.rb | 4 ++-- activesupport/lib/active_support/cache.rb | 4 ++-- .../lib/active_support/testing/performance.rb | 8 ++++---- activesupport/test/core_ext/duration_test.rb | 4 ++-- activesupport/test/ordered_hash_test.rb | 4 ++-- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb index ef0df9d6a8c4d..16ad354773d28 100644 --- a/actionpack/test/controller/assert_select_test.rb +++ b/actionpack/test/controller/assert_select_test.rb @@ -461,8 +461,8 @@ def test_assert_select_rjs_for_remove_offers_useful_error_when_assertion_fails assert_select_rjs :remove, "test1" - rescue Assertion - assert_equal "No RJS statement that removes 'test1' was rendered.", $!.message + rescue Assertion => e + assert_equal "No RJS statement that removes 'test1' was rendered.", e.message end def test_assert_select_rjs_for_remove_ignores_block @@ -493,8 +493,8 @@ def test_assert_select_rjs_for_show_offers_useful_error_when_assertion_fails assert_select_rjs :show, "test1" - rescue Assertion - assert_equal "No RJS statement that shows 'test1' was rendered.", $!.message + rescue Assertion => e + assert_equal "No RJS statement that shows 'test1' was rendered.", e.message end def test_assert_select_rjs_for_show_ignores_block @@ -525,8 +525,8 @@ def test_assert_select_rjs_for_hide_offers_useful_error_when_assertion_fails assert_select_rjs :hide, "test1" - rescue Assertion - assert_equal "No RJS statement that hides 'test1' was rendered.", $!.message + rescue Assertion => e + assert_equal "No RJS statement that hides 'test1' was rendered.", e.message end def test_assert_select_rjs_for_hide_ignores_block @@ -557,8 +557,8 @@ def test_assert_select_rjs_for_toggle_offers_useful_error_when_assertion_fails assert_select_rjs :toggle, "test1" - rescue Assertion - assert_equal "No RJS statement that toggles 'test1' was rendered.", $!.message + rescue Assertion => e + assert_equal "No RJS statement that toggles 'test1' was rendered.", e.message end def test_assert_select_rjs_for_toggle_ignores_block diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb index 8e74eff0abd78..ec7035e540b9f 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb @@ -67,8 +67,8 @@ def self.establish_connection(spec = nil) begin require "active_record/connection_adapters/#{spec[:adapter]}_adapter" - rescue LoadError - raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{$!})" + rescue LoadError => e + raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{e})" end adapter_method = "#{spec[:adapter]}_connection" diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index b1aad0d496aa9..436edeab346a1 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -45,8 +45,8 @@ namespace :db do # Create the SQLite database ActiveRecord::Base.establish_connection(config) ActiveRecord::Base.connection - rescue - $stderr.puts $!, *($!.backtrace) + rescue Exception => e + $stderr.puts e, *(e.backtrace) $stderr.puts "Couldn't create database for #{config.inspect}" end end @@ -91,8 +91,8 @@ namespace :db do ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public')) ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding)) ActiveRecord::Base.establish_connection(config) - rescue - $stderr.puts $!, *($!.backtrace) + rescue Exception => e + $stderr.puts e, *(e.backtrace) $stderr.puts "Couldn't create database for #{config.inspect}" end end diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 7963aa462f15b..46a14b4b945f9 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -589,8 +589,8 @@ def prefix_source() "#{value}" end def prefix(options={}) "#{prefix_call}" end RUBY_EVAL end - rescue - logger.error "Couldn't set prefix: #{$!}\n #{code}" if logger + rescue Exception => e + logger.error "Couldn't set prefix: #{e}\n #{code}" if logger raise end diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index df35540b55759..9098ffbfec28d 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -61,8 +61,8 @@ def self.lookup_store(*store_option) store_class = begin require "active_support/cache/#{store}" - rescue LoadError - raise "Could not find cache store adapter for #{store} (#{$!})" + rescue LoadError => e + raise "Could not find cache store adapter for #{store} (#{e})" else ActiveSupport::Cache.const_get(store_class_name) end diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb index f7ddf6421dd03..64b436ba8ce23 100644 --- a/activesupport/lib/active_support/testing/performance.rb +++ b/activesupport/lib/active_support/testing/performance.rb @@ -58,16 +58,16 @@ def run_test(metric, mode) metric.send(mode) { __send__ @method_name } rescue ::Test::Unit::AssertionFailedError => e add_failure(e.message, e.backtrace) - rescue StandardError, ScriptError - add_error($!) + rescue StandardError, ScriptError => e + add_error(e) ensure begin teardown run_callbacks :teardown, :enumerator => :reverse_each rescue ::Test::Unit::AssertionFailedError => e add_failure(e.message, e.backtrace) - rescue StandardError, ScriptError - add_error($!) + rescue StandardError, ScriptError => e + add_error(e) end end diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index b6456f0a30990..bb453b8d7f334 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -53,8 +53,8 @@ def test_argument_error 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 - flunk("ArgumentError should be raised, but we got #{$!.class} instead") + rescue Exception => e + flunk("ArgumentError should be raised, but we got #{e.class} instead") end end diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index f47e896487800..50778b5864c93 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -217,8 +217,8 @@ def test_alternate_initialization_raises_exception_on_odd_length_args ActiveSupport::OrderedHash[1,2,3,4,5] flunk "Hash::[] should have raised an exception on initialization " + "with an odd number of parameters" - rescue - assert_equal "odd number of arguments for Hash", $!.message + rescue ArgumentError => e + assert_equal "odd number of arguments for Hash", e.message end end