Permalink
Browse files

Make sure the wrapped exceptions also have the original exception ava…

…ilable.

[#2419 state:committed]
  • Loading branch information...
1 parent 00a5fd3 commit b5dfdc714fab7d2836e0a979ca88b4a17db9ec06 @NZKoz NZKoz committed Jun 26, 2009
View
15 activerecord/lib/active_record/base.rb
@@ -67,12 +67,23 @@ class RecordNotSaved < ActiveRecordError
class StatementInvalid < ActiveRecordError
end
+ # Parent class for all specific exceptions which wrap database driver exceptions
+ # provides access to the original exception also.
+ class WrappedDatabaseException < StatementInvalid
+ attr_reader :original_exception
+
+ def initialize(message, original_exception)
+ super(message)
+ @original_exception, = original_exception
+ end
+ end
+
# Raised when a record cannot be inserted because it would violate a uniqueness constraint.
- class RecordNotUnique < StatementInvalid
+ class RecordNotUnique < WrappedDatabaseException
end
# Raised when a record cannot be inserted or updated because it references a non-existent record.
- class InvalidForeignKey < StatementInvalid
+ class InvalidForeignKey < WrappedDatabaseException
end
# Raised when number of bind variables in statement given to <tt>:condition</tt> key (for example, when using +find+ method)
View
4 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -568,9 +568,9 @@ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
def translate_exception(exception, message)
case exception.errno
when 1062
- RecordNotUnique.new(message)
+ RecordNotUnique.new(message, exception)
when 1452
- InvalidForeignKey.new(message)
+ InvalidForeignKey.new(message, exception)
else
super
end
View
4 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -944,9 +944,9 @@ def postgresql_version
def translate_exception(exception, message)
case exception.message
when /duplicate key value violates unique constraint/
- RecordNotUnique.new(message)
+ RecordNotUnique.new(message, exception)
when /violates foreign key constraint/
- InvalidForeignKey.new(message)
+ InvalidForeignKey.new(message, exception)
else
super
end
View
2 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -435,7 +435,7 @@ def default_primary_key_type
def translate_exception(exception, message)
case exception.message
when /column(s)? .* (is|are) not unique/
- RecordNotUnique.new(message)
+ RecordNotUnique.new(message, exception)
else
super
end

0 comments on commit b5dfdc7

Please sign in to comment.