提交 6efd02e2 编写于 作者: R Ryuta Kamizono

Add `ActiveRecord::ValueTooLong` exception class

上级 b9a95456
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册