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


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f682b2cc
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f682b2cc
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f682b2cc

Branch: refs/heads/master
Commit: f682b2cc50b679d799876489bd784c317ec45326
Parents: cd5e924
Author: Benjamin Mahler <bmah...@apache.org>
Authored: Tue Sep 20 14:14:39 2016 -0700
Committer: Benjamin Mahler <bmah...@apache.org>
Committed: Wed Sep 21 14:12:18 2016 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/uuid.hpp | 19 ++++++++++++-------
 3rdparty/stout/tests/uuid_tests.cpp   |  8 +++++---
 2 files changed, 17 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/f682b2cc/3rdparty/stout/include/stout/uuid.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/uuid.hpp 
b/3rdparty/stout/include/stout/uuid.hpp
index e638d45..b84299c 100644
--- a/3rdparty/stout/include/stout/uuid.hpp
+++ b/3rdparty/stout/include/stout/uuid.hpp
@@ -15,6 +15,7 @@
 
 #include <assert.h>
 
+#include <stdexcept>
 #include <string>
 
 #include <boost/uuid/random_generator.hpp>
@@ -69,14 +70,18 @@ public:
     return UUID(uuid);
   }
 
-  static UUID fromString(const std::string& s)
+  static Try<UUID> fromString(const std::string& s)
   {
-    // 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);
+    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

http://git-wip-us.apache.org/repos/asf/mesos/blob/f682b2cc/3rdparty/stout/tests/uuid_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/uuid_tests.cpp 
b/3rdparty/stout/tests/uuid_tests.cpp
index 8f28fcf..fe9894a 100644
--- a/3rdparty/stout/tests/uuid_tests.cpp
+++ b/3rdparty/stout/tests/uuid_tests.cpp
@@ -49,9 +49,9 @@ TEST(UUIDTest, Test)
   EXPECT_EQ(string2, string3);
   EXPECT_EQ(string1, string3);
 
-  EXPECT_EQ(uuid1, UUID::fromString(string1));
-  EXPECT_EQ(uuid2, UUID::fromString(string2));
-  EXPECT_EQ(uuid3, UUID::fromString(string3));
+  EXPECT_SOME_EQ(uuid1, UUID::fromString(string1));
+  EXPECT_SOME_EQ(uuid2, UUID::fromString(string2));
+  EXPECT_SOME_EQ(uuid3, UUID::fromString(string3));
 }
 
 
@@ -60,4 +60,6 @@ TEST(UUIDTest, MalformedUUID)
   EXPECT_SOME(UUID::fromBytes(UUID::random().toBytes()));
   EXPECT_ERROR(UUID::fromBytes("malformed-uuid"));
   EXPECT_ERROR(UUID::fromBytes("invalidstringmsg"));
+
+  EXPECT_ERROR(UUID::fromString("malformed-uuid"));
 }

Reply via email to