add type info to broken promise what()
authorJohn Sherwood <jrsherwood@fb.com>
Mon, 7 Dec 2015 20:05:28 +0000 (12:05 -0800)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Mon, 7 Dec 2015 21:20:19 +0000 (13:20 -0800)
Summary: when a promise is broken, add type info to the message. This will allow for some small improvements to debugging errors from broken promises, namely giving developers a slightly more refined target to grep for.

Reviewed By: fugalh

Differential Revision: D2725575

fb-gh-sync-id: b92edbde874e63eaeab97395da7bb52f76968800

folly/futures/FutureException.h
folly/futures/detail/Core.h
folly/futures/test/PromiseTest.cpp

index 1e004dcf167a88b37a9a97762aa3b3370669244e..b7effcd313d4e02d0f4e56cbd177a0e4e69449e3 100644 (file)
@@ -48,8 +48,9 @@ public:
 
 class BrokenPromise : public FutureException {
   public:
-    explicit BrokenPromise() :
-      FutureException("Broken promise") { }
+    explicit BrokenPromise(std::string type) :
+      FutureException(
+          (std::string("Broken promise for type name `") + type) + '`') { }
 };
 
 class NoState : public FutureException {
index b0bb2ff778ddea99ee09139ae9a36426688971dc..773da5e0e146ee4cb3669fc93b5a50df3d7dd724 100644 (file)
@@ -216,7 +216,7 @@ class Core {
     // detachPromise() and setResult() should never be called in parallel
     // so we don't need to protect this.
     if (UNLIKELY(!result_)) {
-      setResult(Try<T>(exception_wrapper(BrokenPromise())));
+      setResult(Try<T>(exception_wrapper(BrokenPromise(typeid(T).name()))));
     }
     detachOne();
   }
index 6338d685f53e013d512724b8e559d95f092b5a1a..743095b8e39b5cc007d14a14351695129dcb0b97 100644 (file)
@@ -126,3 +126,34 @@ TEST(Promise, isFulfilledWithFuture) {
   p.setValue(42); // after here
   EXPECT_TRUE(p.isFulfilled());
 }
+
+TEST(Promise, brokenOnDelete) {
+  auto p = folly::make_unique<Promise<int>>();
+  auto f = p->getFuture();
+
+  EXPECT_FALSE(f.isReady());
+
+  p.reset();
+
+  EXPECT_TRUE(f.isReady());
+
+  auto t = f.getTry();
+
+  EXPECT_TRUE(t.hasException<BrokenPromise>());
+}
+
+TEST(Promise, brokenPromiseHasTypeInfo) {
+  auto pInt = folly::make_unique<Promise<int>>();
+  auto fInt = pInt->getFuture();
+
+  auto pFloat = folly::make_unique<Promise<float>>();
+  auto fFloat = pFloat->getFuture();
+
+  pInt.reset();
+  pFloat.reset();
+
+  auto whatInt = fInt.getTry().exception().what();
+  auto whatFloat = fFloat.getTry().exception().what();
+
+  EXPECT_NE(whatInt, whatFloat);
+}