8000 Use serializeWithErrorT instead of serialize in formatter (#17964) · cloudhub-js/arangodb@9e8566c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e8566c

Browse files
authored
Use serializeWithErrorT instead of serialize in formatter (arangodb#17964)
Jenkins failure unrelated to change.
1 parent c64ff03 commit 9e8566c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/Inspection/Format.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#include <fmt/format.h>
2828

2929
#include "Inspection/VPackSaveInspector.h"
30+
#include "Inspection/VPackWithErrorT.h"
3031
#include "Inspection/detail/traits.h"
31-
#include <Inspection/VPack.h>
3232

3333
template<>
3434
struct fmt::formatter<VPackSlice> {
@@ -77,8 +77,16 @@ struct inspection_formatter : fmt::formatter<VPackSlice> {
7777
typename Inspector = VPackSaveInspector<NoContext>>
7878
requires detail::HasInspectOverload<T, Inspector>::value auto format(
7979
const T& value, FormatContext& ctx) const -> decltype(ctx.out()) {
80-
auto sharedSlice = arangodb::velocypack::serialize(value);
81-
return fmt::formatter<VPackSlice>::format(sharedSlice.slice(), ctx);
80+
auto sharedSlice = inspection::serializeWithErrorT(value);
81+
if (not sharedSlice.ok()) {
82+
VPackBuilder error;
83+
{
84+
VPackObjectBuilder ob(&error);
85+
error.add("error", VPackValue(sharedSlice.error().error()));
86+
}
87+
return fmt::formatter<VPackSlice>::format(error.slice(), ctx);
88+
}
89+
return fmt::formatter<VPackSlice>::format(sharedSlice.get().slice(), ctx);
8290
}
8391
};
8492
} // namespace arangodb::inspection

tests/Basics/InspectionTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,13 @@ auto inspect(Inspector& f, MyStringEnum& x) {
563563
return f.enumeration(x).values(MyStringEnum::kValue1, "value1", //
564564
MyStringEnum::kValue2, "value2");
565565
}
566+
} // namespace
567+
568+
template<>
569+
struct fmt::formatter<MyStringEnum>
570+
: arangodb::inspection::inspection_formatter {};
566571

572+
namespace {
567573
enum class MyIntEnum {
568574
kValue1,
569575
kValue2,
@@ -2673,6 +2679,12 @@ TEST_F(VPackInspectionTest, formatter) {
26732679
"true,\n \"s\" : \"cheese\"\n}");
26742680
}
26752681

2682+
TEST_F(VPackInspectionTest, formatter_prints_serialization_error) {
2683+
MyStringEnum val = static_cast<MyStringEnum>(42);
2684+
auto def = fmt::format("{}", val);
2685+
ASSERT_EQ(def, R"({"error":"Unknown enum value 42"})");
2686+
}
2687+
26762688
TEST_F(VPackInspectionTest, deserialize) {
26772689
velocypack::Builder builder;
26782690
builder.openObject();

0 commit comments

Comments
 (0)
0