Skip to content

Commit f172e2b

Browse files
JacobSzwejbkafacebook-github-bot
authored andcommitted
Log when a Result is created from Error::ok (pytorch#8577)
Summary: If this ctor is firing someone is probably returning Error::ok from a function that should return a Result not an Error. I found it a little hard to debug when this was happening so adding logging to hopefully save someone else time in the future. Differential Revision: D69870978
1 parent 00c1443 commit f172e2b

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

runtime/core/result.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ class Result final {
5959
* a non-Ok value.
6060
*/
6161
/* implicit */ Result(Error error)
62-
: error_(error == Error::Ok ? Error::Internal : error),
63-
hasValue_(false) {}
62+
: error_(error == Error::Ok ? Error::Internal : error), hasValue_(false) {
63+
if ET_UNLIKELY (error == Error::Ok) {
64+
ET_LOG(
65+
Info,
66+
"Attempted to create Result from Error::Ok, this has been converted to Error::Internal.");
67+
}
68+
}
6469

6570
/// Value copy constructor.
6671
/* implicit */ Result(const T& val) : value_(val), hasValue_(true) {}

runtime/core/test/error_handling_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ TEST(ErrorHandlingTest, ResultBasic) {
110110
}
111111

112112
TEST(ErrorHandlingTest, OkErrorNotPossible) {
113+
executorch::runtime::runtime_init();
113114
Result<uint32_t> r(Error::Ok);
114115
ASSERT_FALSE(r.ok());
115116
ASSERT_NE(r.error(), Error::Ok);

0 commit comments

Comments
 (0)