mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
c8ce345964
commit
ad0bde58d4
3 changed files with 39 additions and 2 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
* Prevent AbstractAdapter from converting exceptions from ActiveSupport::Notification
|
||||||
|
callbacks into ActiveRecord::StatementInvalids.
|
||||||
|
|
||||||
|
*Dennis Taylor*
|
||||||
|
|
||||||
* Fix eager loading/preloading association with scope including joins.
|
* Fix eager loading/preloading association with scope including joins.
|
||||||
|
|
||||||
Fixes #28324.
|
Fixes #28324.
|
||||||
|
|
|
@ -576,12 +576,14 @@ module ActiveRecord
|
||||||
type_casted_binds: type_casted_binds,
|
type_casted_binds: type_casted_binds,
|
||||||
statement_name: statement_name,
|
statement_name: statement_name,
|
||||||
connection_id: object_id) do
|
connection_id: object_id) do
|
||||||
|
begin
|
||||||
@lock.synchronize do
|
@lock.synchronize do
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
|
rescue => e
|
||||||
|
raise translate_exception_class(e, sql)
|
||||||
end
|
end
|
||||||
rescue => e
|
end
|
||||||
raise translate_exception_class(e, sql)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def translate_exception(exception, message)
|
def translate_exception(exception, message)
|
||||||
|
|
|
@ -211,6 +211,28 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
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
|
def test_select_all_always_return_activerecord_result
|
||||||
result = @connection.select_all "SELECT * FROM posts"
|
result = @connection.select_all "SELECT * FROM posts"
|
||||||
assert result.is_a?(ActiveRecord::Result)
|
assert result.is_a?(ActiveRecord::Result)
|
||||||
|
@ -261,6 +283,14 @@ module ActiveRecord
|
||||||
assert_not_nil error.message
|
assert_not_nil error.message
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
class AdapterForeignKeyTest < ActiveRecord::TestCase
|
class AdapterForeignKeyTest < ActiveRecord::TestCase
|
||||||
|
|
Loading…
Reference in a new issue