exception refinements for bson split
This commit is contained in:
parent
a4c72ffa5d
commit
45d3b91882
5
HISTORY
5
HISTORY
|
@ -1,4 +1,9 @@
|
||||||
0.20
|
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).
|
* BSON-related code extracted into two separate gems: bson and bson_ext (Chuck Remes).
|
||||||
* Extensions compile on Rubinius (Chuck Remes).
|
* Extensions compile on Rubinius (Chuck Remes).
|
||||||
* Query :fields options allows for values of 0 to exclude fields (houdini, railsjedi).
|
* Query :fields options allows for values of 0 to exclude fields (houdini, railsjedi).
|
||||||
|
|
|
@ -65,7 +65,7 @@ static VALUE MaxKey;
|
||||||
static VALUE Regexp;
|
static VALUE Regexp;
|
||||||
static VALUE RegexpOfHolding;
|
static VALUE RegexpOfHolding;
|
||||||
static VALUE OrderedHash;
|
static VALUE OrderedHash;
|
||||||
static VALUE InvalidName;
|
static VALUE InvalidKeyName;
|
||||||
static VALUE InvalidStringEncoding;
|
static VALUE InvalidStringEncoding;
|
||||||
static VALUE InvalidDocument;
|
static VALUE InvalidDocument;
|
||||||
static VALUE DigestMD5;
|
static VALUE DigestMD5;
|
||||||
|
@ -182,12 +182,12 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
|
||||||
int i;
|
int i;
|
||||||
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
|
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
|
||||||
buffer_free(buffer);
|
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++) {
|
for (i = 0; i < RSTRING_LEN(key); i++) {
|
||||||
if (RSTRING_PTR(key)[i] == '.') {
|
if (RSTRING_PTR(key)[i] == '.') {
|
||||||
buffer_free(buffer);
|
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"));
|
Regexp = rb_const_get(rb_cObject, rb_intern("Regexp"));
|
||||||
RegexpOfHolding = rb_const_get(bson, rb_intern("RegexpOfHolding"));
|
RegexpOfHolding = rb_const_get(bson, rb_intern("RegexpOfHolding"));
|
||||||
rb_require("bson/exceptions");
|
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"));
|
InvalidStringEncoding = rb_const_get(bson, rb_intern("InvalidStringEncoding"));
|
||||||
InvalidDocument = rb_const_get(bson, rb_intern("InvalidDocument"));
|
InvalidDocument = rb_const_get(bson, rb_intern("InvalidDocument"));
|
||||||
rb_require("bson/ordered_hash");
|
rb_require("bson/ordered_hash");
|
||||||
|
|
|
@ -129,10 +129,10 @@ module BSON
|
||||||
k = k.to_s
|
k = k.to_s
|
||||||
if check_keys
|
if check_keys
|
||||||
if k[0] == ?$
|
if k[0] == ?$
|
||||||
raise InvalidName.new("key #{k} must not start with '$'")
|
raise InvalidKeyName.new("key #{k} must not start with '$'")
|
||||||
end
|
end
|
||||||
if k.include? ?.
|
if k.include? ?.
|
||||||
raise InvalidName.new("key #{k} must not contain '.'")
|
raise InvalidKeyName.new("key #{k} must not contain '.'")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
type = bson_type(v)
|
type = bson_type(v)
|
||||||
|
|
|
@ -35,5 +35,5 @@ module BSON
|
||||||
class InvalidDocument < BSONError; end
|
class InvalidDocument < BSONError; end
|
||||||
|
|
||||||
# Raised when an invalid name is used.
|
# Raised when an invalid name is used.
|
||||||
class InvalidName < BSONError; end
|
class InvalidKeyName < BSONError; end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,7 +26,7 @@ module Mongo
|
||||||
# @param [DB] db a MongoDB database instance.
|
# @param [DB] db a MongoDB database instance.
|
||||||
# @param [String, Symbol] name the name of the collection.
|
# @param [String, Symbol] name the name of the collection.
|
||||||
#
|
#
|
||||||
# @raise [InvalidName]
|
# @raise [InvalidNSName]
|
||||||
# if collection name is empty, contains '$', or starts or ends with '.'
|
# if collection name is empty, contains '$', or starts or ends with '.'
|
||||||
#
|
#
|
||||||
# @raise [TypeError]
|
# @raise [TypeError]
|
||||||
|
@ -45,13 +45,13 @@ module Mongo
|
||||||
name = name.to_s
|
name = name.to_s
|
||||||
|
|
||||||
if name.empty? or name.include? ".."
|
if name.empty? or name.include? ".."
|
||||||
raise Mongo::InvalidName, "collection names cannot be empty"
|
raise Mongo::InvalidNSName, "collection names cannot be empty"
|
||||||
end
|
end
|
||||||
if name.include? "$"
|
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
|
end
|
||||||
if name.match(/^\./) or name.match(/\.$/)
|
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
|
end
|
||||||
|
|
||||||
@db, @name = db, name
|
@db, @name = db, name
|
||||||
|
@ -66,7 +66,7 @@ module Mongo
|
||||||
# @param [String] name
|
# @param [String] name
|
||||||
# the collection to return
|
# the collection to return
|
||||||
#
|
#
|
||||||
# @raise [InvalidName]
|
# @raise [Mongo::InvalidNSName]
|
||||||
# if passed an invalid collection name
|
# if passed an invalid collection name
|
||||||
#
|
#
|
||||||
# @return [Collection]
|
# @return [Collection]
|
||||||
|
@ -545,7 +545,7 @@ module Mongo
|
||||||
#
|
#
|
||||||
# @param [String] new_name the new name for this collection
|
# @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)
|
def rename(new_name)
|
||||||
case new_name
|
case new_name
|
||||||
when Symbol, String
|
when Symbol, String
|
||||||
|
@ -556,13 +556,13 @@ module Mongo
|
||||||
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 InvalidName, "collection names cannot be empty"
|
raise Mongo::InvalidNSName, "collection names cannot be empty"
|
||||||
end
|
end
|
||||||
if new_name.include? "$"
|
if new_name.include? "$"
|
||||||
raise InvalidName, "collection names must not contain '$'"
|
raise Mongo::InvalidNSName, "collection names must not contain '$'"
|
||||||
end
|
end
|
||||||
if new_name.match(/^\./) or new_name.match(/\.$/)
|
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
|
end
|
||||||
|
|
||||||
@db.rename_collection(@name, new_name)
|
@db.rename_collection(@name, new_name)
|
||||||
|
|
|
@ -24,27 +24,20 @@ module Mongo
|
||||||
# Raised when configuration options cause connections, queries, etc., to fail.
|
# Raised when configuration options cause connections, queries, etc., to fail.
|
||||||
class ConfigurationError < MongoRubyError; end
|
class ConfigurationError < MongoRubyError; end
|
||||||
|
|
||||||
# Raised with fatal errors to GridFS.
|
# Raised on fatal errors to GridFS.
|
||||||
class GridError < MongoRubyError; end
|
class GridError < MongoRubyError; end
|
||||||
|
|
||||||
# Raised when invalid arguments are sent to Mongo Ruby methods.
|
# Raised when invalid arguments are sent to Mongo Ruby methods.
|
||||||
class MongoArgumentError < MongoRubyError; end
|
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.
|
# Raised on failures in connection to the database server.
|
||||||
class ConnectionError < MongoRubyError; end
|
class ConnectionError < MongoRubyError; end
|
||||||
|
|
||||||
# Raised on failures in connection to the database server.
|
# Raised on failures in connection to the database server.
|
||||||
class ConnectionTimeoutError < MongoRubyError; end
|
class ConnectionTimeoutError < MongoRubyError; end
|
||||||
|
|
||||||
# Raised when trying to insert a document that exceeds the 4MB limit or
|
# Raised when a connection operation fails.
|
||||||
# when the document contains objects that can't be serialized as BSON.
|
class ConnectionFailure < MongoDBError; end
|
||||||
class InvalidDocument < MongoDBError; end
|
|
||||||
|
|
||||||
# Raised when authentication fails.
|
# Raised when authentication fails.
|
||||||
class AuthenticationError < MongoDBError; end
|
class AuthenticationError < MongoDBError; end
|
||||||
|
@ -52,14 +45,11 @@ module Mongo
|
||||||
# Raised when a database operation fails.
|
# Raised when a database operation fails.
|
||||||
class OperationFailure < MongoDBError; end
|
class OperationFailure < MongoDBError; end
|
||||||
|
|
||||||
# Raised when a connection operation fails.
|
|
||||||
class ConnectionFailure < MongoDBError; end
|
|
||||||
|
|
||||||
# Raised when a client attempts to perform an invalid operation.
|
# Raised when a client attempts to perform an invalid operation.
|
||||||
class InvalidOperation < MongoDBError; end
|
class InvalidOperation < MongoDBError; end
|
||||||
|
|
||||||
# Raised when an invalid name is used.
|
# Raised when an invalid collection or database name is used (invalid namespace name).
|
||||||
class InvalidName < RuntimeError; end
|
class InvalidNSName < RuntimeError; end
|
||||||
|
|
||||||
# Raised when the client supplies an invalid value to sort by.
|
# Raised when the client supplies an invalid value to sort by.
|
||||||
class InvalidSortValueError < MongoRubyError; end
|
class InvalidSortValueError < MongoRubyError; end
|
||||||
|
|
|
@ -49,10 +49,10 @@ module Mongo
|
||||||
|
|
||||||
[" ", ".", "$", "/", "\\"].each do |invalid_char|
|
[" ", ".", "$", "/", "\\"].each do |invalid_char|
|
||||||
if db_name.include? 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
|
||||||
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
|
db_name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,11 +27,11 @@ class TestCollection < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_valid_names
|
def test_valid_names
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
@@db["te$t"]
|
@@db["te$t"]
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
@@db['$main']
|
@@db['$main']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -32,12 +32,12 @@ class TestConnection < Test::Unit::TestCase
|
||||||
def test_invalid_database_names
|
def test_invalid_database_names
|
||||||
assert_raise TypeError do @mongo.db(4) end
|
assert_raise TypeError do @mongo.db(4) end
|
||||||
|
|
||||||
assert_raise Mongo::InvalidName do @mongo.db('') end
|
assert_raise Mongo::InvalidNSName do @mongo.db('') end
|
||||||
assert_raise Mongo::InvalidName do @mongo.db('te$t') end
|
assert_raise Mongo::InvalidNSName do @mongo.db('te$t') end
|
||||||
assert_raise Mongo::InvalidName do @mongo.db('te.t') end
|
assert_raise Mongo::InvalidNSName do @mongo.db('te.t') end
|
||||||
assert_raise Mongo::InvalidName do @mongo.db('te\\t') end
|
assert_raise Mongo::InvalidNSName do @mongo.db('te\\t') end
|
||||||
assert_raise Mongo::InvalidName do @mongo.db('te/t') end
|
assert_raise Mongo::InvalidNSName do @mongo.db('te/t') end
|
||||||
assert_raise Mongo::InvalidName do @mongo.db('te st') end
|
assert_raise Mongo::InvalidNSName do @mongo.db('te st') end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_database_info
|
def test_database_info
|
||||||
|
|
|
@ -3,6 +3,7 @@ require 'test/test_helper'
|
||||||
# NOTE: assumes Mongo is running
|
# NOTE: assumes Mongo is running
|
||||||
class DBAPITest < Test::Unit::TestCase
|
class DBAPITest < Test::Unit::TestCase
|
||||||
include Mongo
|
include Mongo
|
||||||
|
include BSON
|
||||||
|
|
||||||
@@conn = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
@@conn = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
||||||
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
|
||||||
|
@ -646,32 +647,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 InvalidName do
|
assert_raise BSON::InvalidKeyName do
|
||||||
@@coll.insert({"$hello" => "world"})
|
@@coll.insert({"$hello" => "world"})
|
||||||
end
|
end
|
||||||
assert_raise InvalidName do
|
assert_raise BSON::InvalidKeyName 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 InvalidName do
|
assert_raise BSON::InvalidKeyName do
|
||||||
@@coll.insert({".hello" => "world"})
|
@@coll.insert({".hello" => "world"})
|
||||||
end
|
end
|
||||||
assert_raise InvalidName do
|
assert_raise BSON::InvalidKeyName do
|
||||||
@@coll.insert({"hello" => {".hello" => "world"}})
|
@@coll.insert({"hello" => {".hello" => "world"}})
|
||||||
end
|
end
|
||||||
assert_raise InvalidName do
|
assert_raise BSON::InvalidKeyName do
|
||||||
@@coll.insert({"hello." => "world"})
|
@@coll.insert({"hello." => "world"})
|
||||||
end
|
end
|
||||||
assert_raise InvalidName do
|
assert_raise BSON::InvalidKeyName do
|
||||||
@@coll.insert({"hello" => {"hello." => "world"}})
|
@@coll.insert({"hello" => {"hello." => "world"}})
|
||||||
end
|
end
|
||||||
assert_raise InvalidName do
|
assert_raise BSON::InvalidKeyName do
|
||||||
@@coll.insert({"hel.lo" => "world"})
|
@@coll.insert({"hel.lo" => "world"})
|
||||||
end
|
end
|
||||||
assert_raise InvalidName do
|
assert_raise BSON::InvalidKeyName do
|
||||||
@@coll.insert({"hello" => {"hel.lo" => "world"}})
|
@@coll.insert({"hello" => {"hel.lo" => "world"}})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -680,19 +681,19 @@ class DBAPITest < Test::Unit::TestCase
|
||||||
assert_raise TypeError do
|
assert_raise TypeError do
|
||||||
@@db.collection(5)
|
@@db.collection(5)
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
@@db.collection("")
|
@@db.collection("")
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
@@db.collection("te$t")
|
@@db.collection("te$t")
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
@@db.collection(".test")
|
@@db.collection(".test")
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
@@db.collection("test.")
|
@@db.collection("test.")
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
@@db.collection("tes..t")
|
@@db.collection("tes..t")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -706,19 +707,19 @@ class DBAPITest < Test::Unit::TestCase
|
||||||
assert_raise TypeError do
|
assert_raise TypeError do
|
||||||
a.rename(5)
|
a.rename(5)
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
a.rename("")
|
a.rename("")
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
a.rename("te$t")
|
a.rename("te$t")
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
a.rename(".test")
|
a.rename(".test")
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
a.rename("test.")
|
a.rename("test.")
|
||||||
end
|
end
|
||||||
assert_raise Mongo::InvalidName do
|
assert_raise Mongo::InvalidNSName do
|
||||||
a.rename("tes..t")
|
a.rename("tes..t")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue