Skip to content

Commit

Permalink
add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
marin-ma committed Nov 21, 2024
1 parent 0d462c4 commit ef29f71
Showing 1 changed file with 66 additions and 23 deletions.
89 changes: 66 additions & 23 deletions velox/common/base/Status.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,49 @@ namespace facebook::velox {
/// VELOX_RETURN_NOT_OK(operation2());
/// VELOX_RETURN_NOT_OK(operation3());
/// ...
///
/// folly::Expected<T, Status> holds either a result or an error status and is
/// aliased as Expected<T> to simplify usage.
///
/// For operations return a value if successful, or error status if not,
/// use Expected<T>:
///
/// Simple usage:
///
/// Expected<bool> operation() {
/// if (noMoreMemory) {
/// return folly::makeUnexpected(
/// Status::OutOfMemory("Not enough memory to run 'operation'!"));
/// }
/// return true;
/// }
///
/// Call site:
///
/// auto expected = operation();
/// if (expected.hasError()) {
/// auto error = expected.error();
/// (handle error status)
/// } else {
/// auto value = expected.value();
/// (handle value on success)
/// }
///
/// The same logic above can be implemented using helper macros:
///
/// Expected<bool> operation() {
/// VELOX_RETURN_UNEXPECTED_IF(noMoreMemory,
/// Status::OutOfMemory("Not enough memory to run 'operation'!"));
/// return true;
/// }
///
/// To ensure operations succeed (or if not, return the same status wrapped in
/// folly::Unexpected from the current function):
///
/// ...
/// VELOX_RETURN_UNEXPECTED(operationReturnExpected());
/// VELOX_RETURN_UNEXPECTED_NOT_OK(operationReturnStatus());
/// ...

/// This enum represents common categories of errors found in the library. These
/// are not meant to cover every specific error situation, but rather cover
Expand Down Expand Up @@ -485,29 +528,6 @@ void Status::moveFrom(Status& s) {
VELOX_RETURN_IF(!__s.ok(), __s); \
} while (false)

/// Return with given status wrapped in folly::Unexpected if condition is met.
#define VELOX_RETURN_UNEXPECTED_IF(condition, status) \
do { \
if (FOLLY_UNLIKELY(condition)) { \
return (::folly::makeUnexpected(status)); \
} \
} while (false)

/// Propagate any non-successful Status wrapped in folly::Unexpected to the
/// caller.
#define VELOX_RETURN_UNEXPECTED_NOT_OK(status) \
do { \
::facebook::velox::Status __s = \
::facebook::velox::internal::genericToStatus(status); \
VELOX_RETURN_IF(!__s.ok(), ::folly::makeUnexpected(__s)); \
} while (false)

#define VELOX_RETURN_UNEXPECTED(expected) \
do { \
auto res = (expected); \
VELOX_RETURN_UNEXPECTED_IF(res.hasError(), res.error()); \
} while (false)

namespace internal {

/// Common API for extracting Status from either Status or Result<T> (the latter
Expand Down Expand Up @@ -539,6 +559,29 @@ inline Status genericToStatus(Status&& st) {
template <typename T>
using Expected = folly::Expected<T, Status>;

/// Return with given status wrapped in folly::Unexpected if condition is met.
#define VELOX_RETURN_UNEXPECTED_IF(condition, status) \
do { \
if (FOLLY_UNLIKELY(condition)) { \
return (::folly::makeUnexpected(status)); \
} \
} while (false)

/// Propagate any non-successful Status wrapped in folly::Unexpected to the
/// caller.
#define VELOX_RETURN_UNEXPECTED_NOT_OK(status) \
do { \
::facebook::velox::Status __s = \
::facebook::velox::internal::genericToStatus(status); \
VELOX_RETURN_IF(!__s.ok(), ::folly::makeUnexpected(__s)); \
} while (false)

#define VELOX_RETURN_UNEXPECTED(expected) \
do { \
auto res = (expected); \
VELOX_RETURN_UNEXPECTED_IF(res.hasError(), res.error()); \
} while (false)

} // namespace facebook::velox

template <>
Expand Down

0 comments on commit ef29f71

Please sign in to comment.