Commit 4e7efffd authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

revise exceptionStr

Summary: Revise all overloads of `folly::exceptionStr` in terms of `type_info_of`, `exception_ptr_get_type`, and `exception_ptr_get_object`. No longer rely on `catch_exception` and no longer have inline preprocessor conditionals.

Reviewed By: Orvid, luciang

Differential Revision: D26333081

fbshipit-source-id: 318ce83b9f15a12d5a33f528134e6fb38bb78a62
parent 93d52d89
...@@ -18,67 +18,34 @@ ...@@ -18,67 +18,34 @@
#include <utility> #include <utility>
#include <folly/CPortability.h>
#include <folly/CppAttributes.h>
#include <folly/Demangle.h> #include <folly/Demangle.h>
#include <folly/functional/Invoke.h>
#include <folly/lang/Exception.h> #include <folly/lang/Exception.h>
#include <folly/lang/TypeInfo.h>
namespace folly { namespace folly {
namespace {
fbstring exception_string_type(std::type_info const* ti) {
return ti ? demangle(*ti) : "<unknown exception>";
}
} // namespace
/** /**
* Debug string for an exception: include type and what(), if * Debug string for an exception: include type and what(), if
* defined. * defined.
*/ */
fbstring exceptionStr(const std::exception& e) { fbstring exceptionStr(std::exception const& e) {
#if FOLLY_HAS_RTTI auto prefix = exception_string_type(folly::type_info_of(e));
fbstring rv(demangle(typeid(e))); return std::move(prefix) + ": " + e.what();
rv += ": ";
#else
fbstring rv("Exception (no RTTI available): ");
#endif
rv += e.what();
return rv;
} }
namespace { fbstring exceptionStr(std::exception_ptr const& ep) {
if (auto ex = exception_ptr_get_object<std::exception>(ep)) {
FOLLY_CREATE_MEMBER_INVOKER(invoke_cxa_exception_type_fn, __cxa_exception_type); return exceptionStr(*ex);
struct fallback_cxa_exception_type_fn {
FOLLY_MAYBE_UNUSED FOLLY_ERASE_HACK_GCC std::type_info const* operator()(
std::exception_ptr const&) const noexcept {
return nullptr;
}
};
using invoke_or_fallback_cxa_exception_type_fn = std::conditional_t<
is_invocable_r_v<
std::type_info const*,
invoke_cxa_exception_type_fn,
std::exception_ptr const&>,
invoke_cxa_exception_type_fn,
fallback_cxa_exception_type_fn>;
FOLLY_INLINE_VARIABLE constexpr invoke_or_fallback_cxa_exception_type_fn
invoke_or_fallback_cxa_exception_type;
} // namespace
fbstring exceptionStr(std::exception_ptr ep) {
if (!kHasExceptions) {
return "Exception (catch unavailable)";
} }
auto type = invoke_or_fallback_cxa_exception_type(ep); return exception_string_type(exception_ptr_get_type(ep));
return catch_exception(
[&]() -> fbstring {
return catch_exception<std::exception const&>(
[&]() -> fbstring { std::rethrow_exception(std::move(ep)); },
static_cast<fbstring (&)(std::exception const&)>(exceptionStr));
},
[&]() -> fbstring {
return type ? demangle(*type) : "<unknown exception>";
});
} }
} // namespace folly } // namespace folly
...@@ -26,8 +26,8 @@ namespace folly { ...@@ -26,8 +26,8 @@ namespace folly {
* Debug string for an exception: include type and what(), if * Debug string for an exception: include type and what(), if
* defined. * defined.
*/ */
fbstring exceptionStr(const std::exception& e); fbstring exceptionStr(std::exception const& e);
fbstring exceptionStr(std::exception_ptr ep); fbstring exceptionStr(std::exception_ptr const& ep);
} // namespace folly } // namespace folly
...@@ -26,15 +26,9 @@ ...@@ -26,15 +26,9 @@
namespace folly { namespace folly {
void Executor::invokeCatchingExnsLog( void Executor::invokeCatchingExnsLog(char const* const prefix) noexcept {
char const* const prefix, std::exception const* const ex) { auto ep = std::current_exception();
auto const message = " threw unhandled "; LOG(ERROR) << prefix << " threw unhandled " << exceptionStr(ep);
if (ex) {
LOG(ERROR) << prefix << message << exceptionStr(*ex);
} else {
auto ep = std::current_exception();
LOG(ERROR) << prefix << message << exceptionStr(ep);
}
} }
void Executor::addWithPriority(Func, int8_t /* priority */) { void Executor::addWithPriority(Func, int8_t /* priority */) {
......
...@@ -232,8 +232,7 @@ class Executor { ...@@ -232,8 +232,7 @@ class Executor {
template <typename F> template <typename F>
FOLLY_ERASE static void invokeCatchingExns(char const* p, F f) noexcept { FOLLY_ERASE static void invokeCatchingExns(char const* p, F f) noexcept {
auto h = [p](auto&... e) noexcept { invokeCatchingExnsLog(p, &e...); }; catch_exception(f, invokeCatchingExnsLog, p);
catch_exception([&] { catch_exception<std::exception const&>(f, h); }, h);
} }
protected: protected:
...@@ -269,8 +268,7 @@ class Executor { ...@@ -269,8 +268,7 @@ class Executor {
} }
private: private:
static void invokeCatchingExnsLog( static void invokeCatchingExnsLog(char const* prefix) noexcept;
char const* prefix, std::exception const* ex = nullptr);
template <typename ExecutorT> template <typename ExecutorT>
static KeepAlive<ExecutorT> makeKeepAliveDummy(ExecutorT* executor) { static KeepAlive<ExecutorT> makeKeepAliveDummy(ExecutorT* executor) {
......
...@@ -30,7 +30,7 @@ TEST_F(ExceptionStringTest, exception_ptr) { ...@@ -30,7 +30,7 @@ TEST_F(ExceptionStringTest, exception_ptr) {
TEST_F(ExceptionStringTest, exception_ptr_unknown) { TEST_F(ExceptionStringTest, exception_ptr_unknown) {
auto ptr = std::make_exception_ptr(7); auto ptr = std::make_exception_ptr(7);
auto expected = folly::kIsLibstdcpp ? "int" : "<unknown exception>"; auto expected = "int";
auto actual = folly::exceptionStr(ptr).toStdString(); auto actual = folly::exceptionStr(ptr).toStdString();
EXPECT_EQ(expected, actual); EXPECT_EQ(expected, actual);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment