mirror of
https://github.com/fmtlib/fmt.git
synced 2026-07-30 16:26:27 +08:00
Format null std::exception_ptr as "none"
Change the null exception_ptr representation from "nullptr" to "none" to convey the "no exception" state more clearly, and update the test.
This commit is contained in:
parent
6dac6cad05
commit
9d13c41ce6
@ -651,16 +651,12 @@ struct formatter<
|
|||||||
|
|
||||||
template <> struct formatter<std::exception_ptr> : formatter<std::exception> {
|
template <> struct formatter<std::exception_ptr> : formatter<std::exception> {
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(const std::exception_ptr& ex_ptr, FormatContext& ctx) const
|
auto format(const std::exception_ptr& ep, FormatContext& ctx) const
|
||||||
-> decltype(ctx.out()) {
|
-> decltype(ctx.out()) {
|
||||||
if (!ex_ptr) {
|
if (!ep) return detail::write(ctx.out(), string_view("none"));
|
||||||
return detail::write(ctx.out(), string_view("nullptr"));
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::rethrow_exception(ex_ptr);
|
std::rethrow_exception(ep);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
// Reuses the base formatter<std::exception>::format behavior perfectly
|
|
||||||
return formatter<std::exception>::format(e, ctx);
|
return formatter<std::exception>::format(e, ctx);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
return detail::write(ctx.out(), string_view("unknown exception"));
|
return detail::write(ctx.out(), string_view("unknown exception"));
|
||||||
|
|||||||
@ -415,7 +415,7 @@ TEST(std_test, exception_ptr) {
|
|||||||
p2 = std::current_exception();
|
p2 = std::current_exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
EXPECT_EQ(fmt::format("{}", p1), "nullptr");
|
EXPECT_EQ(fmt::format("{}", p1), "none");
|
||||||
EXPECT_EQ(fmt::format("{}", p2), "My Exception");
|
EXPECT_EQ(fmt::format("{}", p2), "My Exception");
|
||||||
|
|
||||||
#if FMT_USE_RTTI
|
#if FMT_USE_RTTI
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user