Skip to content

Commit

Permalink
revise exceptionStr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yfeldblum authored and facebook-github-bot committed May 2, 2021
1 parent 93d52d8 commit 4e7efff
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 65 deletions.
65 changes: 16 additions & 49 deletions folly/ExceptionString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions folly/ExceptionString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 3 additions & 9 deletions folly/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */) {
Expand Down
6 changes: 2 additions & 4 deletions folly/Executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion folly/test/ExceptionStringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 4e7efff

Please sign in to comment.