diff options
author | Jacob Evans <jacob.evans@10gen.com> | 2020-04-01 22:06:09 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-05-23 01:05:19 +0000 |
commit | d169769bba283cd0a1906c81580ff8e7cf7bcd93 (patch) | |
tree | 8b8a90beecd579aeb0f2a4ae564b4a7995081d60 /src/mongo/db/exec | |
parent | b3153fcc446c6fcf4c102dd53e5295c51fc73c16 (diff) | |
download | mongo-d169769bba283cd0a1906c81580ff8e7cf7bcd93.tar.gz |
SERVER-47030 Fix date_time_support to not throw exceptions
Diffstat (limited to 'src/mongo/db/exec')
-rw-r--r-- | src/mongo/db/exec/document_value/document_value_test.cpp | 7 | ||||
-rw-r--r-- | src/mongo/db/exec/document_value/value.cpp | 15 |
2 files changed, 19 insertions, 3 deletions
diff --git a/src/mongo/db/exec/document_value/document_value_test.cpp b/src/mongo/db/exec/document_value/document_value_test.cpp index cdf80a59562..7f7dea26ef4 100644 --- a/src/mongo/db/exec/document_value/document_value_test.cpp +++ b/src/mongo/db/exec/document_value/document_value_test.cpp @@ -30,6 +30,7 @@ #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kDefault #include <math.h> +#include <sstream> #include "mongo/platform/basic.h" @@ -2357,4 +2358,10 @@ public: OldStyleSuiteInitializer<All> myall; +TEST(ValueOutput, StreamOutputForIllegalDateProducesErrorToken) { + auto sout = std::ostringstream{}; + sout << mongo::Value{Date_t::min()}; + ASSERT_EQ("illegal date", sout.str()); +} + } // namespace DocumentTests diff --git a/src/mongo/db/exec/document_value/value.cpp b/src/mongo/db/exec/document_value/value.cpp index f0d9f684222..7afef48204d 100644 --- a/src/mongo/db/exec/document_value/value.cpp +++ b/src/mongo/db/exec/document_value/value.cpp @@ -57,6 +57,7 @@ using std::ostream; using std::string; using std::stringstream; using std::vector; +using namespace std::string_literals; constexpr StringData Value::kISOFormatString; @@ -634,7 +635,9 @@ string Value::coerceToString() const { return getTimestamp().toStringPretty(); case Date: - return TimeZoneDatabase::utcZone().formatDate(Value::kISOFormatString, getDate()); + return uassertStatusOKWithContext( + TimeZoneDatabase::utcZone().formatDate(Value::kISOFormatString, getDate()), + "failed while coercing date to string"); case EOO: case jstNULL: @@ -1170,8 +1173,14 @@ ostream& operator<<(ostream& out, const Value& val) { case Undefined: return out << "undefined"; case Date: - return out << TimeZoneDatabase::utcZone().formatDate(Value::kISOFormatString, - val.coerceToDate()); + return out << [&] { + if (auto string = TimeZoneDatabase::utcZone().formatDate(Value::kISOFormatString, + val.coerceToDate()); + string.isOK()) + return string.getValue(); + else + return "illegal date"s; + }(); case bsonTimestamp: return out << val.getTimestamp().toString(); case Object: |