From 0e7c824d239f6668be1afb2ffd334f89ce294c80 Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Fri, 13 Jan 2017 12:16:51 -0800 Subject: [PATCH] capture exception information when creating exception_wrapper Summary: prefer creating an exception_wrapper with a reference to the active exception Depends on D4410421 Reviewed By: spacedentist Differential Revision: D4410455 fbshipit-source-id: d6b6aeb5fa72782e31d754a0b853514af5fdb8cd --- folly/futures/detail/Core.h | 8 +++++++- folly/futures/test/PromiseTest.cpp | 2 ++ folly/futures/test/SharedPromiseTest.cpp | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/folly/futures/detail/Core.h b/folly/futures/detail/Core.h index 41fcdab2..be8e4b99 100644 --- a/folly/futures/detail/Core.h +++ b/folly/futures/detail/Core.h @@ -356,6 +356,7 @@ class Core final { } if (x) { + exception_wrapper ew; try { if (LIKELY(x->getNumPriorities() == 1)) { x->add([core_ref = CountedReference(this)]() mutable { @@ -374,10 +375,15 @@ class Core final { core->callback_(std::move(*core->result_)); }, priority); } + } catch (const std::exception& e) { + ew = exception_wrapper(std::current_exception(), e); } catch (...) { + ew = exception_wrapper(std::current_exception()); + } + if (ew) { CountedReference core_ref(this); RequestContextScopeGuard rctx(context_); - result_ = Try(exception_wrapper(std::current_exception())); + result_ = Try(std::move(ew)); SCOPE_EXIT { callback_ = {}; }; callback_(std::move(*result_)); } diff --git a/folly/futures/test/PromiseTest.cpp b/folly/futures/test/PromiseTest.cpp index 559391a2..ff9cef9c 100644 --- a/folly/futures/test/PromiseTest.cpp +++ b/folly/futures/test/PromiseTest.cpp @@ -92,6 +92,8 @@ TEST(Promise, setException) { auto f = p.getFuture(); try { throw eggs; + } catch (const std::exception& e) { + p.setException(exception_wrapper(std::current_exception(), e)); } catch (...) { p.setException(exception_wrapper(std::current_exception())); } diff --git a/folly/futures/test/SharedPromiseTest.cpp b/folly/futures/test/SharedPromiseTest.cpp index 7a4ea320..a449bf60 100644 --- a/folly/futures/test/SharedPromiseTest.cpp +++ b/folly/futures/test/SharedPromiseTest.cpp @@ -146,8 +146,8 @@ TEST(SharedPromise, splitFutureFailure) { EXPECT_FALSE(f1.isReady()); try { throw std::runtime_error("Oops"); - } catch (...) { - p.setException(exception_wrapper(std::current_exception())); + } catch (const std::exception& e) { + p.setException(exception_wrapper(std::current_exception(), e)); } EXPECT_TRUE(f1.isReady()); EXPECT_TRUE(f1.hasException()); -- 2.34.1