Add ActiveRecord::ValueTooLong
exception class
This commit is contained in:
parent
b9a95456a5
commit
6efd02e230
@ -741,6 +741,8 @@ def translate_exception(exception, message)
|
||||
RecordNotUnique.new(message)
|
||||
when 1452
|
||||
InvalidForeignKey.new(message)
|
||||
when 1406
|
||||
ValueTooLong.new(message)
|
||||
else
|
||||
super
|
||||
end
|
||||
|
@ -398,6 +398,7 @@ def postgresql_version
|
||||
protected
|
||||
|
||||
# See http://www.postgresql.org/docs/current/static/errcodes-appendix.html
|
||||
VALUE_LIMIT_VIOLATION = "22001"
|
||||
FOREIGN_KEY_VIOLATION = "23503"
|
||||
UNIQUE_VIOLATION = "23505"
|
||||
|
||||
@ -409,6 +410,8 @@ def translate_exception(exception, message)
|
||||
RecordNotUnique.new(message)
|
||||
when FOREIGN_KEY_VIOLATION
|
||||
InvalidForeignKey.new(message)
|
||||
when VALUE_LIMIT_VIOLATION
|
||||
ValueTooLong.new(message)
|
||||
else
|
||||
super
|
||||
end
|
||||
|
@ -125,6 +125,10 @@ class RecordNotUnique < WrappedDatabaseException
|
||||
class InvalidForeignKey < WrappedDatabaseException
|
||||
end
|
||||
|
||||
# Raised when a record cannot be inserted or updated because a value too long for a column type.
|
||||
class ValueTooLong < StatementInvalid
|
||||
end
|
||||
|
||||
# Raised when number of bind variables in statement given to +:condition+ key
|
||||
# (for example, when using {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] method)
|
||||
# does not match number of expected values supplied.
|
||||
|
@ -2,6 +2,7 @@
|
||||
require "models/book"
|
||||
require "models/post"
|
||||
require "models/author"
|
||||
require "models/event"
|
||||
|
||||
module ActiveRecord
|
||||
class AdapterTest < ActiveRecord::TestCase
|
||||
@ -193,6 +194,14 @@ def test_foreign_key_violations_are_translated_to_specific_exception_with_valida
|
||||
|
||||
assert_not_nil error.cause
|
||||
end
|
||||
|
||||
def test_value_limit_violations_are_translated_to_specific_exception
|
||||
error = assert_raises(ActiveRecord::ValueTooLong) do
|
||||
Event.create(title: 'abcdefgh')
|
||||
end
|
||||
|
||||
assert_not_nil error.cause
|
||||
end
|
||||
end
|
||||
|
||||
def test_disable_referential_integrity
|
||||
|
Loading…
Reference in New Issue
Block a user