diff options
author | Matthew Draper <matthew@trebex.net> | 2017-07-09 04:14:48 +0930 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-09 04:14:48 +0930 |
commit | e5bcc7b53cb87643e8cdbf000fea0a03bfcd34f6 (patch) | |
tree | 84809d2b8d6edc196037aae2dcdc57125b0c5f65 | |
parent | 40a20ded015c92c875c8e96c1687c96303ced7f8 (diff) | |
parent | 8219e177633e3c3a77a50c2342b8fe6c4956914b (diff) | |
download | rails-e5bcc7b53cb87643e8cdbf000fea0a03bfcd34f6.tar.gz rails-e5bcc7b53cb87643e8cdbf000fea0a03bfcd34f6.tar.bz2 rails-e5bcc7b53cb87643e8cdbf000fea0a03bfcd34f6.zip |
Merge pull request #29692 from fimmtiu/avoid-translating-non-database-exceptions
Don't translate non-database exceptions.
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/adapter_test.rb | 22 |
3 files changed, 31 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 8d900d9669..53b9752867 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* Prevent errors raised by sql.active_record notification subscribers from being converted into + ActiveRecord::StatementInvalid exceptions. + + *Dennis Taylor* + * Fix eager loading/preloading association with scope including joins. Fixes #28324. diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index cfe1892d78..30b29e7007 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -576,12 +576,14 @@ module ActiveRecord type_casted_binds: type_casted_binds, statement_name: statement_name, connection_id: object_id) do + begin @lock.synchronize do yield end + rescue => e + raise translate_exception_class(e, sql) end - rescue => e - raise translate_exception_class(e, sql) + end end def translate_exception(exception, message) diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index a1fb6427f9..827bcba121 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 = StandardError.new("This StandardError shouldn't get translated") + subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") { raise original_error } + actual_error = assert_raises(StandardError) do + @connection.execute("SELECT * FROM posts") + end + + assert_equal original_error, actual_error + + ensure + ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber + end + + def test_database_related_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_kind_of Exception, error.cause + end + def test_select_all_always_return_activerecord_result result = @connection.select_all "SELECT * FROM posts" assert result.is_a?(ActiveRecord::Result) |