Raise better error message when trying to serialize TimeWithZone
This commit is contained in:
parent
d1d1f68c2f
commit
8c6e0a3591
|
@ -369,13 +369,13 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
||||||
SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&obj_length, 4);
|
SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&obj_length, 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (strcmp(cls, "DateTime") == 0 || strcmp(cls, "Date") == 0) {
|
if (strcmp(cls, "DateTime") == 0 || strcmp(cls, "Date") == 0 || strcmp(cls, "ActiveSupport::TimeWithZone") == 0) {
|
||||||
buffer_free(buffer);
|
buffer_free(buffer);
|
||||||
rb_raise(InvalidDocument, "Trying to use Date or DateTime; the driver currently supports Time objects only.",
|
rb_raise(InvalidDocument, "Trying to serialize and instance of Date, DateTime, or TimeWithZone; the MongoDB Ruby driver currently supports Time objects only.",
|
||||||
TYPE(value));
|
TYPE(value));
|
||||||
}
|
}
|
||||||
buffer_free(buffer);
|
buffer_free(buffer);
|
||||||
rb_raise(InvalidDocument, "Unsupported type for BSON (%d)", TYPE(value));
|
rb_raise(InvalidDocument, "Cannot serialize an object of class %s into BSON.", cls);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case T_DATA:
|
case T_DATA:
|
||||||
|
@ -428,8 +428,9 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
|
const char* cls = rb_class2name(RBASIC(value)->klass);
|
||||||
buffer_free(buffer);
|
buffer_free(buffer);
|
||||||
rb_raise(InvalidDocument, "Unsupported type for BSON (%d)", TYPE(value));
|
rb_raise(InvalidDocument, "Cannot serialize an object of class %s into BSON.", cls);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -554,9 +554,15 @@ class BSON_RUBY
|
||||||
when Symbol
|
when Symbol
|
||||||
SYMBOL
|
SYMBOL
|
||||||
when Date, DateTime
|
when Date, DateTime
|
||||||
raise InvalidDocument, "Trying to use Date or DateTime; the driver currently supports Time objects only."
|
raise InvalidDocument, "Trying to serialize an instance of #{o.class}; " +
|
||||||
|
"the MongoDB Ruby driver currently supports Time objects only."
|
||||||
else
|
else
|
||||||
raise InvalidDocument, "Unknown type of object: #{o.class.name}"
|
if defined?(ActiveSupport::TimeWithZone) && o.is_a?(ActiveSupport::TimeWithZone)
|
||||||
|
raise InvalidDocument, "Trying to serialize an instance of ActiveSupport::TimeWithZone; " +
|
||||||
|
"the MongoDB Ruby driver currently supports Time objects only."
|
||||||
|
else
|
||||||
|
raise InvalidDocument, "Unknown type of object: #{o.class.name}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
# encoding:utf-8
|
# encoding:utf-8
|
||||||
require 'test/test_helper'
|
require 'test/test_helper'
|
||||||
|
|
||||||
|
# Need to simulating this class
|
||||||
|
# without actually loading it.
|
||||||
|
module ActiveSupport
|
||||||
|
class TimeWithZone
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class BSONTest < Test::Unit::TestCase
|
class BSONTest < Test::Unit::TestCase
|
||||||
|
|
||||||
include Mongo
|
include Mongo
|
||||||
|
@ -181,15 +188,16 @@ class BSONTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exeption_on_using_date
|
def test_exeption_on_using_unsupported_date_class
|
||||||
[DateTime.now, Date.today].each do |invalid_date|
|
[DateTime.now, Date.today, ActiveSupport::TimeWithZone.new].each do |invalid_date|
|
||||||
doc = {:date => invalid_date}
|
doc = {:date => invalid_date}
|
||||||
begin
|
begin
|
||||||
bson = BSON.serialize(doc)
|
bson = BSON.serialize(doc)
|
||||||
rescue => e
|
rescue => e
|
||||||
ensure
|
ensure
|
||||||
|
puts e.message
|
||||||
assert_equal InvalidDocument, e.class
|
assert_equal InvalidDocument, e.class
|
||||||
assert_match /Time/, e.message
|
assert_match /Time objects only/, e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue