Skip to content

Commit 89b56e1

Browse files
Neural-Link Teamtensorflow-copybara
authored andcommitted
Add a RET_CHECK_TRUE and rename RET_CHECK to RET_CHECK_OK.
PiperOrigin-RevId: 367285228
1 parent 090da90 commit 89b56e1

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

research/carls/base/status_helper.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,16 @@ class MakeErrorStream {
8888
return *this;
8989
}
9090

91-
// Adds RET_CHECK failure text to error message.
92-
MakeErrorStreamWithOutput& add_ret_check_failure(const char* condition) {
93-
return *this << "RET_CHECK failure (" << impl_->file_ << ":" << impl_->line_
94-
<< ") " << condition << " ";
91+
// Adds RET_CHECK_OK failure text to error message.
92+
MakeErrorStreamWithOutput& add_ret_check_ok_failure(const char* condition) {
93+
return *this << "RET_CHECK_OK failure (" << impl_->file_ << ":"
94+
<< impl_->line_ << ") " << condition << " ";
95+
}
96+
97+
// Adds RET_CHECK_TRUE failure text to error message.
98+
MakeErrorStreamWithOutput& add_ret_check_true_failure() {
99+
return *this << "RET_CHECK_TRUE failure (" << impl_->file_ << ":"
100+
<< impl_->line_ << ") ";
95101
}
96102

97103
private:
@@ -175,15 +181,23 @@ absl::Status ToAbslStatus(const tensorflow::Status& status);
175181
// Converts from absl::Status to grpc::Status.
176182
grpc::Status ToGrpcStatus(const absl::Status& status);
177183

178-
// Checks if given condition returns Ok status, if not, returns an error status
184+
// Checks if given condition returns Ok status. If not, returns an error status
179185
// and stack trace.
180-
#undef RET_CHECK
181-
#define RET_CHECK(condition) \
186+
#undef RET_CHECK_OK
187+
#define RET_CHECK_OK(condition) \
182188
while (TF_PREDICT_FALSE(!(condition.ok()))) \
183189
return carls::internal::MakeErrorStream(__FILE__, __LINE__, \
184190
absl::StatusCode::kInternal) \
185191
.with_log_stack_trace() \
186-
.add_ret_check_failure(#condition)
192+
.add_ret_check_ok_failure(#condition)
193+
194+
// Checks if given condition returns true. If not, returns the stack trace.
195+
#define RET_CHECK_TRUE(condition) \
196+
while (TF_PREDICT_FALSE(!(condition))) \
197+
return carls::internal::MakeErrorStream(__FILE__, __LINE__, \
198+
absl::StatusCode::kInternal) \
199+
.with_log_stack_trace() \
200+
.add_ret_check_true_failure()
187201

188202
} // namespace carls
189203

research/carls/base/status_helper_test.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace carls {
2525
namespace {
2626

2727
absl::Status TestRetCheck(const absl::Status& status) {
28-
RET_CHECK(status) << status.message();
28+
RET_CHECK_OK(status) << status.message();
2929
return absl::OkStatus();
3030
}
3131

@@ -59,7 +59,7 @@ TEST(ProtoFactoryTest, ToGrpcStatus) {
5959
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, grpc_status.error_code());
6060
}
6161

62-
TEST(ProtoFactoryTest, RetCheck) {
62+
TEST(ProtoFactoryTest, RetCheckOk) {
6363
auto status = TestRetCheck(absl::InvalidArgumentError("My Error message."));
6464
EXPECT_TRUE(absl::StrContains(status.message(),
6565
"research/carls/base/status_helper_test.cc"));
@@ -68,4 +68,17 @@ TEST(ProtoFactoryTest, RetCheck) {
6868
EXPECT_OK(TestRetCheck(absl::OkStatus()));
6969
}
7070

71+
TEST(ProtoFactoryTest, RetCheckTrue) {
72+
auto lambda = [](bool condition) -> absl::Status {
73+
RET_CHECK_TRUE(condition) << "My Errors.";
74+
return absl::OkStatus();
75+
};
76+
auto status = lambda(false);
77+
EXPECT_TRUE(absl::StrContains(status.message(),
78+
"research/carls/base/status_helper_test.cc"));
79+
EXPECT_TRUE(absl::StrContains(status.message(), "My Errors."));
80+
81+
EXPECT_OK(lambda(true));
82+
}
83+
7184
} // namespace carls

0 commit comments

Comments
 (0)