diff options
author | Dennis Taylor <dennis.taylor@clio.com> | 2017-07-05 15:21:25 -0700 |
---|---|---|
committer | Dennis Taylor <dennis.taylor@clio.com> | 2017-07-05 17:02:59 -0700 |
commit | ad0bde58d4e5dd3686fdbdc21c7e4b6d71e371e4 (patch) | |
tree | 2d267c1ae61cb23758cc4ed6ff1edf4a9d054460 /activerecord/test/cases | |
parent | c8ce3459648ce0f86646b564ce1c0bb16a4b48eb (diff) | |
download | rails-ad0bde58d4e5dd3686fdbdc21c7e4b6d71e371e4.tar.gz rails-ad0bde58d4e5dd3686fdbdc21c7e4b6d71e371e4.tar.bz2 rails-ad0bde58d4e5dd3686fdbdc21c7e4b6d71e371e4.zip |
Don't translate non-database exceptions.
The AbstractAdapter will translate all StandardErrors generated during the course of a query into ActiveRecord::StatementInvalids. Unfortunately, it'll also mangle non-database-related errors generated in ActiveSupport::Notification callbacks after the query has successfully completed. This should prevent it from translating errors from ActiveSupport::Notifications.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/adapter_test.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index a1fb6427f9..6d211d5768 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -211,6 +211,28 @@ module ActiveRecord end end + def test_exceptions_from_notifications_are_not_translated + original_error = RuntimeError.new("This RuntimeError shouldn't get translated") + subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") { raise original_error } + actual_error = assert_raises(RuntimeError) do + @connection.execute("SELECT * FROM posts") + end + + assert_equal original_error, actual_error + + ensure + ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber + end + + def test_other_exceptions_are_translated_to_statement_invalid + error = assert_raises(ActiveRecord::StatementInvalid) do + @connection.execute("This is a syntax error") + end + + assert_instance_of ActiveRecord::StatementInvalid, error + assert_instance_of syntax_error_exception_class, error.cause + end + def test_select_all_always_return_activerecord_result result = @connection.select_all "SELECT * FROM posts" assert result.is_a?(ActiveRecord::Result) @@ -261,6 +283,14 @@ module ActiveRecord assert_not_nil error.message end end + + private + + def syntax_error_exception_class + return Mysql2::Error if defined?(Mysql2) + return PG::SyntaxError if defined?(PG) + return SQLite3::SQLException if defined?(SQLite3) + end end class AdapterForeignKeyTest < ActiveRecord::TestCase |