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 @@
#include <utility>
#include <folly/CPortability.h>
#include <folly/CppAttributes.h>
#include <folly/Demangle.h>
#include <folly/functional/Invoke.h>
#include <folly/lang/Exception.h>
#include <folly/lang/TypeInfo.h>
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
* defined.
*/
fbstring exceptionStr(const std::exception& e) {
#if FOLLY_HAS_RTTI
fbstring rv(demangle(typeid(e)));
rv += ": ";
#else
fbstring rv("Exception (no RTTI available): ");
#endif
rv += e.what();
return rv;
fbstring exceptionStr(std::exception const& e) {
auto prefix = exception_string_type(folly::type_info_of(e));
return std::move(prefix) + ": " + e.what();
}
namespace {
FOLLY_CREATE_MEMBER_INVOKER(invoke_cxa_exception_type_fn, __cxa_exception_type);
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)";
fbstring exceptionStr(std::exception_ptr const& ep) {
if (auto ex = exception_ptr_get_object<std::exception>(ep)) {
return exceptionStr(*ex);
}
auto type = invoke_or_fallback_cxa_exception_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>";
});
return exception_string_type(exception_ptr_get_type(ep));
}
} // namespace folly
......@@ -26,8 +26,8 @@ namespace folly {
* Debug string for an exception: include type and what(), if
* 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
......@@ -26,15 +26,9 @@
namespace folly {
void Executor::invokeCatchingExnsLog(
char const* const prefix, std::exception const* const ex) {
auto const message = " threw unhandled ";
if (ex) {
LOG(ERROR) << prefix << message << exceptionStr(*ex);
} else {
auto ep = std::current_exception();
LOG(ERROR) << prefix << message << exceptionStr(ep);
}
void Executor::invokeCatchingExnsLog(char const* const prefix) noexcept {
auto ep = std::current_exception();
LOG(ERROR) << prefix << " threw unhandled " << exceptionStr(ep);
}
void Executor::addWithPriority(Func, int8_t /* priority */) {
......
......@@ -232,8 +232,7 @@ class Executor {
template <typename F>
FOLLY_ERASE static void invokeCatchingExns(char const* p, F f) noexcept {
auto h = [p](auto&... e) noexcept { invokeCatchingExnsLog(p, &e...); };
catch_exception([&] { catch_exception<std::exception const&>(f, h); }, h);
catch_exception(f, invokeCatchingExnsLog, p);
}
protected:
......@@ -269,8 +268,7 @@ class Executor {
}
private:
static void invokeCatchingExnsLog(
char const* prefix, std::exception const* ex = nullptr);
static void invokeCatchingExnsLog(char const* prefix) noexcept;
template <typename ExecutorT>
static KeepAlive<ExecutorT> makeKeepAliveDummy(ExecutorT* executor) {
......
......@@ -30,7 +30,7 @@ TEST_F(ExceptionStringTest, exception_ptr) {
TEST_F(ExceptionStringTest, exception_ptr_unknown) {
auto ptr = std::make_exception_ptr(7);
auto expected = folly::kIsLibstdcpp ? "int" : "<unknown exception>";
auto expected = "int";
auto actual = folly::exceptionStr(ptr).toStdString();
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