exception refinements for bson split

This commit is contained in:
Kyle Banker 2010-04-05 18:24:31 -04:00
parent a4c72ffa5d
commit 45d3b91882
10 changed files with 55 additions and 59 deletions

View File

@ -1,4 +1,9 @@
0.20
* Exception class adjustments:
* Mongo::InvalidObjectID moved to BSON::InvalidObjectID
* Mongo::InvalidDocument moved to BSON::InvalidDocument
* Mongo::InvalidStringEncoding moved to BSON::InvalidStringEncoding
* Mongo::InvalidName replaced by Mongo::InvalidNSName and BSON::InvalidKeyName
* BSON-related code extracted into two separate gems: bson and bson_ext (Chuck Remes).
* Extensions compile on Rubinius (Chuck Remes).
* Query :fields options allows for values of 0 to exclude fields (houdini, railsjedi).

View File

@ -65,7 +65,7 @@ static VALUE MaxKey;
static VALUE Regexp;
static VALUE RegexpOfHolding;
static VALUE OrderedHash;
static VALUE InvalidName;
static VALUE InvalidKeyName;
static VALUE InvalidStringEncoding;
static VALUE InvalidDocument;
static VALUE DigestMD5;
@ -182,12 +182,12 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
int i;
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
buffer_free(buffer);
rb_raise(InvalidName, "key must not start with '$'");
rb_raise(InvalidKeyName, "key must not start with '$'");
}
for (i = 0; i < RSTRING_LEN(key); i++) {
if (RSTRING_PTR(key)[i] == '.') {
buffer_free(buffer);
rb_raise(InvalidName, "key must not contain '.'");
rb_raise(InvalidKeyName, "key must not contain '.'");
}
}
}
@ -902,7 +902,7 @@ void Init_cbson() {
Regexp = rb_const_get(rb_cObject, rb_intern("Regexp"));
RegexpOfHolding = rb_const_get(bson, rb_intern("RegexpOfHolding"));
rb_require("bson/exceptions");
InvalidName = rb_const_get(bson, rb_intern("InvalidName"));
InvalidKeyName = rb_const_get(bson, rb_intern("InvalidKeyName"));
InvalidStringEncoding = rb_const_get(bson, rb_intern("InvalidStringEncoding"));
InvalidDocument = rb_const_get(bson, rb_intern("InvalidDocument"));
rb_require("bson/ordered_hash");

View File

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

View File

@ -35,5 +35,5 @@ module BSON
class InvalidDocument < BSONError; end
# Raised when an invalid name is used.
class InvalidName < BSONError; end
class InvalidKeyName < BSONError; end
end

View File

@ -26,7 +26,7 @@ module Mongo
# @param [DB] db a MongoDB database instance.
# @param [String, Symbol] name the name of the collection.
#
# @raise [InvalidName]
# @raise [InvalidNSName]
# if collection name is empty, contains '$', or starts or ends with '.'
#
# @raise [TypeError]
@ -45,13 +45,13 @@ module Mongo
name = name.to_s
if name.empty? or name.include? ".."
raise Mongo::InvalidName, "collection names cannot be empty"
raise Mongo::InvalidNSName, "collection names cannot be empty"
end
if name.include? "$"
raise Mongo::InvalidName, "collection names must not contain '$'" unless name =~ /((^\$cmd)|(oplog\.\$main))/
raise Mongo::InvalidNSName, "collection names must not contain '$'" unless name =~ /((^\$cmd)|(oplog\.\$main))/
end
if name.match(/^\./) or name.match(/\.$/)
raise Mongo::InvalidName, "collection names must not start or end with '.'"
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
end
@db, @name = db, name
@ -66,7 +66,7 @@ module Mongo
# @param [String] name
# the collection to return
#
# @raise [InvalidName]
# @raise [Mongo::InvalidNSName]
# if passed an invalid collection name
#
# @return [Collection]
@ -545,7 +545,7 @@ module Mongo
#
# @param [String] new_name the new name for this collection
#
# @raise [InvalidName] if +new_name+ is an invalid collection name.
# @raise [Mongo::InvalidNSName] if +new_name+ is an invalid collection name.
def rename(new_name)
case new_name
when Symbol, String
@ -556,13 +556,13 @@ module Mongo
new_name = new_name.to_s
if new_name.empty? or new_name.include? ".."
raise InvalidName, "collection names cannot be empty"
raise Mongo::InvalidNSName, "collection names cannot be empty"
end
if new_name.include? "$"
raise InvalidName, "collection names must not contain '$'"
raise Mongo::InvalidNSName, "collection names must not contain '$'"
end
if new_name.match(/^\./) or new_name.match(/\.$/)
raise InvalidName, "collection names must not start or end with '.'"
raise Mongo::InvalidNSName, "collection names must not start or end with '.'"
end
@db.rename_collection(@name, new_name)

View File

@ -24,27 +24,20 @@ module Mongo
# Raised when configuration options cause connections, queries, etc., to fail.
class ConfigurationError < MongoRubyError; end
# Raised with fatal errors to GridFS.
# Raised on fatal errors to GridFS.
class GridError < MongoRubyError; end
# Raised when invalid arguments are sent to Mongo Ruby methods.
class MongoArgumentError < MongoRubyError; end
# Raised when given a string is not valid utf-8 (Ruby 1.8 only).
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.
class ConnectionError < MongoRubyError; end
# Raised on failures in connection to the database server.
class ConnectionTimeoutError < MongoRubyError; end
# Raised when trying to insert a document that exceeds the 4MB limit or
# when the document contains objects that can't be serialized as BSON.
class InvalidDocument < MongoDBError; end
# Raised when a connection operation fails.
class ConnectionFailure < MongoDBError; end
# Raised when authentication fails.
class AuthenticationError < MongoDBError; end
@ -52,14 +45,11 @@ module Mongo
# Raised when a database operation fails.
class OperationFailure < MongoDBError; end
# Raised when a connection operation fails.
class ConnectionFailure < MongoDBError; end
# Raised when a client attempts to perform an invalid operation.
class InvalidOperation < MongoDBError; end
# Raised when an invalid name is used.
class InvalidName < RuntimeError; end
# Raised when an invalid collection or database name is used (invalid namespace name).
class InvalidNSName < RuntimeError; end
# Raised when the client supplies an invalid value to sort by.
class InvalidSortValueError < MongoRubyError; end

View File

@ -49,10 +49,10 @@ module Mongo
[" ", ".", "$", "/", "\\"].each do |invalid_char|
if db_name.include? invalid_char
raise InvalidName, "database names cannot contain the character '#{invalid_char}'"
raise Mongo::InvalidNSName, "database names cannot contain the character '#{invalid_char}'"
end
end
raise InvalidName, "database name cannot be the empty string" if db_name.empty?
raise Mongo::InvalidNSName, "database name cannot be the empty string" if db_name.empty?
db_name
end
end

View File

@ -27,11 +27,11 @@ class TestCollection < Test::Unit::TestCase
end
def test_valid_names
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
@@db["te$t"]
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
@@db['$main']
end

View File

@ -32,12 +32,12 @@ class TestConnection < Test::Unit::TestCase
def test_invalid_database_names
assert_raise TypeError do @mongo.db(4) end
assert_raise Mongo::InvalidName do @mongo.db('') end
assert_raise Mongo::InvalidName do @mongo.db('te$t') end
assert_raise Mongo::InvalidName do @mongo.db('te.t') end
assert_raise Mongo::InvalidName do @mongo.db('te\\t') end
assert_raise Mongo::InvalidName do @mongo.db('te/t') end
assert_raise Mongo::InvalidName do @mongo.db('te st') end
assert_raise Mongo::InvalidNSName do @mongo.db('') end
assert_raise Mongo::InvalidNSName do @mongo.db('te$t') end
assert_raise Mongo::InvalidNSName do @mongo.db('te.t') end
assert_raise Mongo::InvalidNSName do @mongo.db('te\\t') end
assert_raise Mongo::InvalidNSName do @mongo.db('te/t') end
assert_raise Mongo::InvalidNSName do @mongo.db('te st') end
end
def test_database_info

View File

@ -3,6 +3,7 @@ require 'test/test_helper'
# NOTE: assumes Mongo is running
class DBAPITest < Test::Unit::TestCase
include Mongo
include BSON
@@conn = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
@ -646,32 +647,32 @@ class DBAPITest < Test::Unit::TestCase
@@coll.insert({"hello" => "world"})
@@coll.insert({"hello" => {"hello" => "world"}})
assert_raise InvalidName do
assert_raise BSON::InvalidKeyName do
@@coll.insert({"$hello" => "world"})
end
assert_raise InvalidName do
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello" => {"$hello" => "world"}})
end
@@coll.insert({"he$llo" => "world"})
@@coll.insert({"hello" => {"hell$o" => "world"}})
assert_raise InvalidName do
assert_raise BSON::InvalidKeyName do
@@coll.insert({".hello" => "world"})
end
assert_raise InvalidName do
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello" => {".hello" => "world"}})
end
assert_raise InvalidName do
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello." => "world"})
end
assert_raise InvalidName do
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello" => {"hello." => "world"}})
end
assert_raise InvalidName do
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hel.lo" => "world"})
end
assert_raise InvalidName do
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello" => {"hel.lo" => "world"}})
end
end
@ -680,19 +681,19 @@ class DBAPITest < Test::Unit::TestCase
assert_raise TypeError do
@@db.collection(5)
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
@@db.collection("")
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
@@db.collection("te$t")
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
@@db.collection(".test")
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
@@db.collection("test.")
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
@@db.collection("tes..t")
end
end
@ -706,19 +707,19 @@ class DBAPITest < Test::Unit::TestCase
assert_raise TypeError do
a.rename(5)
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
a.rename("")
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
a.rename("te$t")
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
a.rename(".test")
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
a.rename("test.")
end
assert_raise Mongo::InvalidName do
assert_raise Mongo::InvalidNSName do
a.rename("tes..t")
end