added custom exception for illegal object ids
This commit is contained in:
parent
6d573edd40
commit
8f2e25f8d3
|
@ -329,7 +329,7 @@ See HISTORY.
|
||||||
|
|
||||||
= Credits
|
= Credits
|
||||||
|
|
||||||
See CREDITS
|
See CREDITS.
|
||||||
|
|
||||||
= License
|
= License
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,9 @@ module Mongo
|
||||||
# Raised when given a string is not valid utf-8 (Ruby 1.8 only).
|
# Raised when given a string is not valid utf-8 (Ruby 1.8 only).
|
||||||
class InvalidStringEncoding < MongoRubyError; end
|
class InvalidStringEncoding < MongoRubyError; end
|
||||||
|
|
||||||
|
# Raised when attempting to initialize an invalid ObjectID.
|
||||||
|
class InvalidObjectID < MongoRubyError; end
|
||||||
|
|
||||||
# Raised on failures in connection to the database server.
|
# Raised on failures in connection to the database server.
|
||||||
class ConnectionError < MongoRubyError; end
|
class ConnectionError < MongoRubyError; end
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ module Mongo
|
||||||
# Given a string representation of an ObjectID, return a new ObjectID
|
# Given a string representation of an ObjectID, return a new ObjectID
|
||||||
# with that value.
|
# with that value.
|
||||||
def self.from_string(str)
|
def self.from_string(str)
|
||||||
raise "illegal ObjectID format" unless legal?(str)
|
raise InvalidObjectID, "illegal ObjectID format" unless legal?(str)
|
||||||
data = []
|
data = []
|
||||||
12.times do |i|
|
12.times do |i|
|
||||||
data[i] = str[i * 2, 2].to_i(16)
|
data[i] = str[i * 2, 2].to_i(16)
|
||||||
|
@ -83,7 +83,7 @@ module Mongo
|
||||||
# removed. If you are not sure that you need this method you should be
|
# removed. If you are not sure that you need this method you should be
|
||||||
# using the regular from_string.
|
# using the regular from_string.
|
||||||
def self.from_string_legacy(str)
|
def self.from_string_legacy(str)
|
||||||
raise "illegal ObjectID format" unless legal?(str)
|
raise InvalidObjectID, "illegal ObjectID format" unless legal?(str)
|
||||||
data = []
|
data = []
|
||||||
BYTE_ORDER.each_with_index { |string_position, data_index|
|
BYTE_ORDER.each_with_index { |string_position, data_index|
|
||||||
data[data_index] = str[string_position * 2, 2].to_i(16)
|
data[data_index] = str[string_position * 2, 2].to_i(16)
|
||||||
|
|
|
@ -79,6 +79,12 @@ class ObjectIDTest < Test::Unit::TestCase
|
||||||
assert_equal @o.to_s, o2.to_s
|
assert_equal @o.to_s, o2.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_illegal_from_string
|
||||||
|
assert_raise InvalidObjectID do
|
||||||
|
ObjectID.from_string("")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_from_string_legacy
|
def test_from_string_legacy
|
||||||
hex_str = @o.to_s_legacy
|
hex_str = @o.to_s_legacy
|
||||||
o2 = ObjectID.from_string_legacy(hex_str)
|
o2 = ObjectID.from_string_legacy(hex_str)
|
||||||
|
@ -87,6 +93,12 @@ class ObjectIDTest < Test::Unit::TestCase
|
||||||
assert_equal @o.to_s, o2.to_s
|
assert_equal @o.to_s, o2.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_illegal_from_string_legacy
|
||||||
|
assert_raise InvalidObjectID do
|
||||||
|
ObjectID.from_string_legacy("")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_legal
|
def test_legal
|
||||||
assert !ObjectID.legal?(nil)
|
assert !ObjectID.legal?(nil)
|
||||||
assert !ObjectID.legal?("fred")
|
assert !ObjectID.legal?("fred")
|
||||||
|
|
Loading…
Reference in New Issue