add InvalidName exception and use that where appropriate

This commit is contained in:
Mike Dirolf 2009-08-13 15:52:11 -04:00
parent a41a88d0f2
commit 431039a82a
5 changed files with 39 additions and 33 deletions

View File

@ -49,6 +49,7 @@ static VALUE DBRef;
static VALUE Code; static VALUE Code;
static VALUE RegexpOfHolding; static VALUE RegexpOfHolding;
static VALUE OrderedHash; static VALUE OrderedHash;
static VALUE InvalidName;
// this sucks. but for some reason these moved around between 1.8 and 1.9 // this sucks. but for some reason these moved around between 1.8 and 1.9
#ifdef ONIGURUMA_H #ifdef ONIGURUMA_H
@ -183,11 +184,11 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
if (check_keys == Qtrue) { if (check_keys == Qtrue) {
int i; int i;
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') { if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
rb_raise(rb_eRuntimeError, "key must not start with '$'"); rb_raise(InvalidName, "key must not start with '$'");
} }
for (i = 0; i < RSTRING_LEN(key); i++) { for (i = 0; i < RSTRING_LEN(key); i++) {
if (RSTRING_PTR(key)[i] == '.') { if (RSTRING_PTR(key)[i] == '.') {
rb_raise(rb_eRuntimeError, "key must not contain '.'"); rb_raise(InvalidName, "key must not contain '.'");
} }
} }
} }
@ -755,6 +756,8 @@ void Init_cbson() {
Code = rb_const_get(driver, rb_intern("Code")); Code = rb_const_get(driver, rb_intern("Code"));
rb_require("mongo/types/regexp_of_holding"); rb_require("mongo/types/regexp_of_holding");
RegexpOfHolding = rb_const_get(driver, rb_intern("RegexpOfHolding")); RegexpOfHolding = rb_const_get(driver, rb_intern("RegexpOfHolding"));
rb_require("mongo/errors");
InvalidName = rb_const_get(driver, rb_intern("InvalidName"));
rb_require("mongo/util/ordered_hash"); rb_require("mongo/util/ordered_hash");
OrderedHash = rb_const_get(rb_cObject, rb_intern("OrderedHash")); OrderedHash = rb_const_get(rb_cObject, rb_intern("OrderedHash"));

View File

@ -29,19 +29,19 @@ module XGen
case name case name
when Symbol, String when Symbol, String
else else
raise RuntimeError, "new_name must be a string or symbol" raise TypeError, "new_name must be a string or symbol"
end end
name = name.to_s name = name.to_s
if name.empty? or name.include? ".." if name.empty? or name.include? ".."
raise RuntimeError, "collection names cannot be empty" raise InvalidName, "collection names cannot be empty"
end end
if name.include? "$" and not name.match(/^\$cmd/) if name.include? "$" and not name.match(/^\$cmd/)
raise RuntimeError, "collection names must not contain '$'" raise InvalidName, "collection names must not contain '$'"
end end
if name.match(/^\./) or name.match(/\.$/) if name.match(/^\./) or name.match(/\.$/)
raise RuntimeError, "collection names must not start or end with '.'" raise InvalidName, "collection names must not start or end with '.'"
end end
@db, @name = db, name @db, @name = db, name
@ -256,7 +256,7 @@ EOS
# Rename this collection. # Rename this collection.
# #
# If operating in auth mode, client must be authorized as an admin to # If operating in auth mode, client must be authorized as an admin to
# perform this operation. Raises an error if +new_name+ is an invalid # perform this operation. Raises +InvalidName+ if +new_name+ is an invalid
# collection name. # collection name.
# #
# :new_name :: new name for this collection # :new_name :: new name for this collection
@ -264,19 +264,19 @@ EOS
case new_name case new_name
when Symbol, String when Symbol, String
else else
raise RuntimeError, "new_name must be a string or symbol" raise TypeError, "new_name must be a string or symbol"
end end
new_name = new_name.to_s new_name = new_name.to_s
if new_name.empty? or new_name.include? ".." if new_name.empty? or new_name.include? ".."
raise RuntimeError, "collection names cannot be empty" raise InvalidName, "collection names cannot be empty"
end end
if new_name.include? "$" if new_name.include? "$"
raise RuntimeError, "collection names must not contain '$'" raise InvalidName, "collection names must not contain '$'"
end end
if new_name.match(/^\./) or new_name.match(/\.$/) if new_name.match(/^\./) or new_name.match(/\.$/)
raise RuntimeError, "collection names must not start or end with '.'" raise InvalidName, "collection names must not start or end with '.'"
end end
@db.rename_collection(@name, new_name) @db.rename_collection(@name, new_name)

View File

@ -19,6 +19,9 @@ module XGen
module Driver module Driver
# Raised when a database operation fails. # Raised when a database operation fails.
class OperationFailure < RuntimeError; end class OperationFailure < RuntimeError; end
# Raised when an invalid name is used.
class InvalidName < RuntimeError; end
end end
end end
end end

View File

@ -104,10 +104,10 @@ class BSON
k = k.to_s k = k.to_s
if check_keys if check_keys
if k[0] == ?$ if k[0] == ?$
raise RuntimeError.new("key #{k} must not start with '$'") raise InvalidName.new("key #{k} must not start with '$'")
end end
if k.include? ?. if k.include? ?.
raise RuntimeError.new("key #{k} must not contain '.'") raise InvalidName.new("key #{k} must not contain '.'")
end end
end end
type = bson_type(v) type = bson_type(v)

View File

@ -690,32 +690,32 @@ class DBAPITest < Test::Unit::TestCase
@@coll.insert({"hello" => "world"}) @@coll.insert({"hello" => "world"})
@@coll.insert({"hello" => {"hello" => "world"}}) @@coll.insert({"hello" => {"hello" => "world"}})
assert_raise RuntimeError do assert_raise InvalidName do
@@coll.insert({"$hello" => "world"}) @@coll.insert({"$hello" => "world"})
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@coll.insert({"hello" => {"$hello" => "world"}}) @@coll.insert({"hello" => {"$hello" => "world"}})
end end
@@coll.insert({"he$llo" => "world"}) @@coll.insert({"he$llo" => "world"})
@@coll.insert({"hello" => {"hell$o" => "world"}}) @@coll.insert({"hello" => {"hell$o" => "world"}})
assert_raise RuntimeError do assert_raise InvalidName do
@@coll.insert({".hello" => "world"}) @@coll.insert({".hello" => "world"})
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@coll.insert({"hello" => {".hello" => "world"}}) @@coll.insert({"hello" => {".hello" => "world"}})
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@coll.insert({"hello." => "world"}) @@coll.insert({"hello." => "world"})
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@coll.insert({"hello" => {"hello." => "world"}}) @@coll.insert({"hello" => {"hello." => "world"}})
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@coll.insert({"hel.lo" => "world"}) @@coll.insert({"hel.lo" => "world"})
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@coll.insert({"hello" => {"hel.lo" => "world"}}) @@coll.insert({"hello" => {"hel.lo" => "world"}})
end end
@ -723,22 +723,22 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_collection_names def test_collection_names
assert_raise RuntimeError do assert_raise TypeError do
@@db.collection(5) @@db.collection(5)
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@db.collection("") @@db.collection("")
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@db.collection("te$t") @@db.collection("te$t")
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@db.collection(".test") @@db.collection(".test")
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@db.collection("test.") @@db.collection("test.")
end end
assert_raise RuntimeError do assert_raise InvalidName do
@@db.collection("tes..t") @@db.collection("tes..t")
end end
end end
@ -749,22 +749,22 @@ class DBAPITest < Test::Unit::TestCase
a = @@db.collection("foo") a = @@db.collection("foo")
b = @@db.collection("bar") b = @@db.collection("bar")
assert_raise RuntimeError do assert_raise TypeError do
a.rename(5) a.rename(5)
end end
assert_raise RuntimeError do assert_raise InvalidName do
a.rename("") a.rename("")
end end
assert_raise RuntimeError do assert_raise InvalidName do
a.rename("te$t") a.rename("te$t")
end end
assert_raise RuntimeError do assert_raise InvalidName do
a.rename(".test") a.rename(".test")
end end
assert_raise RuntimeError do assert_raise InvalidName do
a.rename("test.") a.rename("test.")
end end
assert_raise RuntimeError do assert_raise InvalidName do
a.rename("tes..t") a.rename("tes..t")
end end