Skip to content

Commit

Permalink
Make sure the wrapped exceptions also have the original exception ava…
Browse files Browse the repository at this point in the history
…ilable.

[#2419 state:committed]
  • Loading branch information
NZKoz committed Jun 26, 2009
1 parent 00a5fd3 commit b5dfdc7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
15 changes: 13 additions & 2 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -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)
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit b5dfdc7

Please sign in to comment.