diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c index 8509ee2..f8fe2e5 100644 --- a/ext/cbson/cbson.c +++ b/ext/cbson/cbson.c @@ -49,6 +49,7 @@ static VALUE DBRef; static VALUE Code; static VALUE RegexpOfHolding; static VALUE OrderedHash; +static VALUE InvalidName; // this sucks. but for some reason these moved around between 1.8 and 1.9 #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) { int i; 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++) { 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")); rb_require("mongo/types/regexp_of_holding"); 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"); OrderedHash = rb_const_get(rb_cObject, rb_intern("OrderedHash")); diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 6a15aa3..cedce42 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -29,19 +29,19 @@ module XGen case name when Symbol, String else - raise RuntimeError, "new_name must be a string or symbol" + raise TypeError, "new_name must be a string or symbol" end name = name.to_s if name.empty? or name.include? ".." - raise RuntimeError, "collection names cannot be empty" + raise InvalidName, "collection names cannot be empty" end if name.include? "$" and not name.match(/^\$cmd/) - raise RuntimeError, "collection names must not contain '$'" + raise InvalidName, "collection names must not contain '$'" end 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 @db, @name = db, name @@ -256,7 +256,7 @@ EOS # Rename this collection. # # 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. # # :new_name :: new name for this collection @@ -264,19 +264,19 @@ EOS case new_name when Symbol, String else - raise RuntimeError, "new_name must be a string or symbol" + raise TypeError, "new_name must be a string or symbol" end new_name = new_name.to_s if new_name.empty? or new_name.include? ".." - raise RuntimeError, "collection names cannot be empty" + raise InvalidName, "collection names cannot be empty" end if new_name.include? "$" - raise RuntimeError, "collection names must not contain '$'" + raise InvalidName, "collection names must not contain '$'" end 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 @db.rename_collection(@name, new_name) diff --git a/lib/mongo/errors.rb b/lib/mongo/errors.rb index 3847ea3..d2b881a 100644 --- a/lib/mongo/errors.rb +++ b/lib/mongo/errors.rb @@ -19,6 +19,9 @@ module XGen module Driver # Raised when a database operation fails. class OperationFailure < RuntimeError; end + + # Raised when an invalid name is used. + class InvalidName < RuntimeError; end end end end diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index a7afaa0..487e7c3 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -104,10 +104,10 @@ class BSON k = k.to_s if check_keys if k[0] == ?$ - raise RuntimeError.new("key #{k} must not start with '$'") + raise InvalidName.new("key #{k} must not start with '$'") end if k.include? ?. - raise RuntimeError.new("key #{k} must not contain '.'") + raise InvalidName.new("key #{k} must not contain '.'") end end type = bson_type(v) diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 7d4eaae..8f0e197 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -690,32 +690,32 @@ class DBAPITest < Test::Unit::TestCase @@coll.insert({"hello" => "world"}) @@coll.insert({"hello" => {"hello" => "world"}}) - assert_raise RuntimeError do + assert_raise InvalidName do @@coll.insert({"$hello" => "world"}) end - assert_raise RuntimeError do + assert_raise InvalidName do @@coll.insert({"hello" => {"$hello" => "world"}}) end @@coll.insert({"he$llo" => "world"}) @@coll.insert({"hello" => {"hell$o" => "world"}}) - assert_raise RuntimeError do + assert_raise InvalidName do @@coll.insert({".hello" => "world"}) end - assert_raise RuntimeError do + assert_raise InvalidName do @@coll.insert({"hello" => {".hello" => "world"}}) end - assert_raise RuntimeError do + assert_raise InvalidName do @@coll.insert({"hello." => "world"}) end - assert_raise RuntimeError do + assert_raise InvalidName do @@coll.insert({"hello" => {"hello." => "world"}}) end - assert_raise RuntimeError do + assert_raise InvalidName do @@coll.insert({"hel.lo" => "world"}) end - assert_raise RuntimeError do + assert_raise InvalidName do @@coll.insert({"hello" => {"hel.lo" => "world"}}) end @@ -723,22 +723,22 @@ class DBAPITest < Test::Unit::TestCase end def test_collection_names - assert_raise RuntimeError do + assert_raise TypeError do @@db.collection(5) end - assert_raise RuntimeError do + assert_raise InvalidName do @@db.collection("") end - assert_raise RuntimeError do + assert_raise InvalidName do @@db.collection("te$t") end - assert_raise RuntimeError do + assert_raise InvalidName do @@db.collection(".test") end - assert_raise RuntimeError do + assert_raise InvalidName do @@db.collection("test.") end - assert_raise RuntimeError do + assert_raise InvalidName do @@db.collection("tes..t") end end @@ -749,22 +749,22 @@ class DBAPITest < Test::Unit::TestCase a = @@db.collection("foo") b = @@db.collection("bar") - assert_raise RuntimeError do + assert_raise TypeError do a.rename(5) end - assert_raise RuntimeError do + assert_raise InvalidName do a.rename("") end - assert_raise RuntimeError do + assert_raise InvalidName do a.rename("te$t") end - assert_raise RuntimeError do + assert_raise InvalidName do a.rename(".test") end - assert_raise RuntimeError do + assert_raise InvalidName do a.rename("test.") end - assert_raise RuntimeError do + assert_raise InvalidName do a.rename("tes..t") end