Updated UUID::fromString to not throw an exception on error.
The exception from the string_generator needs to be caught so
that we can surface a Try to the caller.
Review: https://reviews.apache.org/r/52098
diff --git a/3rdparty/stout/include/stout/uuid.hpp b/3rdparty/stout/include/stout/uuid.hpp
index a57896c..c3932e2 100644
--- a/3rdparty/stout/include/stout/uuid.hpp
+++ b/3rdparty/stout/include/stout/uuid.hpp
@@ -16,6 +16,7 @@
#include <assert.h>
#include <sstream>
+#include <stdexcept>
#include <string>
#include <boost/uuid/uuid.hpp>
@@ -69,12 +70,18 @@
return UUID(uuid);
}
- static UUID fromString(const std::string& s)
+ static Try<UUID> fromString(const std::string& s)
{
- boost::uuids::uuid uuid;
- std::istringstream in(s);
- in >> uuid;
- return UUID(uuid);
+ try {
+ // NOTE: We don't use THREAD_LOCAL for the `string_generator`
+ // (unlike for the `random_generator` above), because it is cheap
+ // to construct one each time.
+ boost::uuids::string_generator gen;
+ boost::uuids::uuid uuid = gen(s);
+ return UUID(uuid);
+ } catch (const std::runtime_error& e) {
+ return Error(e.what());
+ }
}
std::string toBytes() const
diff --git a/3rdparty/stout/tests/uuid_tests.cpp b/3rdparty/stout/tests/uuid_tests.cpp
index b061b82..ce25faa 100644
--- a/3rdparty/stout/tests/uuid_tests.cpp
+++ b/3rdparty/stout/tests/uuid_tests.cpp
@@ -45,9 +45,9 @@
string string2 = uuid2.toString();
string string3 = uuid3.toString();
- EXPECT_EQ(string1, string2);
- EXPECT_EQ(string2, string3);
- EXPECT_EQ(string1, string3);
+ EXPECT_SOME_EQ(uuid1, UUID::fromString(string1));
+ EXPECT_SOME_EQ(uuid2, UUID::fromString(string2));
+ EXPECT_SOME_EQ(uuid3, UUID::fromString(string3));
}
@@ -56,4 +56,6 @@
EXPECT_SOME(UUID::fromBytes(UUID::random().toBytes()));
EXPECT_ERROR(UUID::fromBytes("malformed-uuid"));
EXPECT_ERROR(UUID::fromBytes("invalidstringmsg"));
+
+ EXPECT_ERROR(UUID::fromString("malformed-uuid"));
}