Skip to content

Commit

Permalink
Properly handle escaping in std::exception formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Aug 27, 2022
1 parent f40db2f commit 1cc09bc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 2 additions & 4 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,11 @@ FMT_BEGIN_NAMESPACE
template <typename Char, typename T>
struct formatter<T, Char,
std::enable_if_t<std::is_base_of_v<std::exception, T>>>
: formatter<basic_string_view<Char>> {
: formatter<std::string> {
template <typename FormatContext>
auto format(const std::exception& ex, FormatContext& ctx) const ->
typename FormatContext::iterator {
auto out = ctx.out();
detail::write(out, ex.what());
return out;
return fmt::formatter<std::string>::format(ex.what(), ctx);
}
};
FMT_END_NAMESPACE
Expand Down
2 changes: 2 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ TEST(std_test, exception) {
std::ignore = vec.at(42);
} catch (const std::exception& ex) {
EXPECT_EQ(fmt::format("{}", ex), "invalid vector subscript");
EXPECT_EQ(fmt::format("{:?}", ex), "\"invalid vector subscript\"");
}

try {
std::vector<int> vec;
std::ignore = vec.at(42);
} catch (const std::out_of_range& ex) {
EXPECT_EQ(fmt::format("{}", ex), "invalid vector subscript");
EXPECT_EQ(fmt::format("{:?}", ex), "\"invalid vector subscript\"");
}
}

0 comments on commit 1cc09bc

Please sign in to comment.