From f793c88e8a15df686d5d7cf6e4f356c7e8a1ab5e Mon Sep 17 00:00:00 2001 From: Jacob Szwejbka Date: Thu, 20 Feb 2025 09:21:44 -0800 Subject: [PATCH] Log when a Result is created from Error::ok (#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. Reviewed By: dbort Differential Revision: D69870978 --- runtime/core/result.h | 9 +++++++-- runtime/core/test/error_handling_test.cpp | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/runtime/core/result.h b/runtime/core/result.h index 7b404bca946..377573e6dfa 100644 --- a/runtime/core/result.h +++ b/runtime/core/result.h @@ -59,8 +59,13 @@ class Result final { * a non-Ok value. */ /* implicit */ Result(Error error) - : error_(error == Error::Ok ? Error::Internal : error), - hasValue_(false) {} + : error_(error == Error::Ok ? Error::Internal : error), hasValue_(false) { + if ET_UNLIKELY (error == Error::Ok) { + ET_LOG( + Debug, + "Attempted to create Result from Error::Ok, this has been converted to Error::Internal."); + } + } /// Value copy constructor. /* implicit */ Result(const T& val) : value_(val), hasValue_(true) {} diff --git a/runtime/core/test/error_handling_test.cpp b/runtime/core/test/error_handling_test.cpp index b6b58623984..ef270cad1ed 100644 --- a/runtime/core/test/error_handling_test.cpp +++ b/runtime/core/test/error_handling_test.cpp @@ -110,6 +110,7 @@ TEST(ErrorHandlingTest, ResultBasic) { } TEST(ErrorHandlingTest, OkErrorNotPossible) { + executorch::runtime::runtime_init(); Result r(Error::Ok); ASSERT_FALSE(r.ok()); ASSERT_NE(r.error(), Error::Ok);