Skip to content

Commit d1cc7e5

Browse files
rlyerlymeta-codesync[bot]
authored andcommitted
Pretty print for interface::Result
Summary: - Add a pretty-print interface for interface::Result so we don't have to manually unpack it to print the error code & message - Use uint8_t as the underlying type for error codes Reviewed By: pbhandar2 Differential Revision: D90684997 fbshipit-source-id: 6da8e72bc5cdd471b05443eede29a2d1b6f8a59b
1 parent 120e35c commit d1cc7e5

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

cachelib/cachebench/runner/CacheComponentStressor.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ CacheComponentStressor::CacheComponentStressor(
104104

105105
auto cache =
106106
RAMCacheComponent::create(std::move(lruConfig), std::move(poolConfig));
107-
XCHECK(cache.hasValue())
108-
<< "Error code: " << static_cast<int8_t>(cache.error().code_) << ": "
109-
<< cache.error().error_;
107+
XCHECK(cache.hasValue()) << cache.error();
110108
cache_ = std::make_unique<RAMCacheComponent>(std::move(cache).value());
111109
}
112110

cachelib/interface/Result.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818

1919
#include <folly/Expected.h>
2020

21+
#include <magic_enum/magic_enum.hpp>
22+
2123
namespace facebook::cachelib::interface {
2224

2325
struct Error {
24-
enum class Code {
26+
enum class Code : uint8_t {
27+
// User errors
2528
INVALID_ARGUMENTS,
2629
INVALID_CONFIG,
2730

@@ -60,3 +63,12 @@ using Result = folly::Expected<Value, Error>;
6063
using UnitResult = Result<folly::Unit>;
6164

6265
} // namespace facebook::cachelib::interface
66+
67+
namespace std {
68+
inline ostream& operator<<(ostream& os,
69+
const facebook::cachelib::interface::Error& error) {
70+
os << "Error (" << magic_enum::enum_name(error.code_) << ", code "
71+
<< static_cast<uint8_t>(error.code_) << "): " << error.error_;
72+
return os;
73+
}
74+
} // namespace std

0 commit comments

Comments
 (0)