Cleanup how transport errors are handled in NN utils
Prior to this change, whenever the NN utility code encountered a HIDL
transport error, the error message would display the file and line
number of the "handleTransportError" function itself. This change
introduces a new macro "HANDLE_TRANSPORT_FAILURE" that handles the
transport error in a similar way but now the error message displays
the file and line number of where the macro is called.
Bug: N/A
Test: mma
Change-Id: I35b34f8f5be52b7fcff0fbb58a37ab2b8c7dd8bb
Merged-In: I35b34f8f5be52b7fcff0fbb58a37ab2b8c7dd8bb
(cherry picked from commit 61f508e018cff89c93a7e08b3dbde1eb6a2155df)
diff --git a/neuralnetworks/utils/common/include/nnapi/hal/HandleError.h b/neuralnetworks/utils/common/include/nnapi/hal/HandleError.h
index e4046b5..78b2a12 100644
--- a/neuralnetworks/utils/common/include/nnapi/hal/HandleError.h
+++ b/neuralnetworks/utils/common/include/nnapi/hal/HandleError.h
@@ -19,65 +19,46 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
+#include <type_traits>
+
namespace android::hardware::neuralnetworks::utils {
template <typename Type>
nn::GeneralResult<Type> handleTransportError(const hardware::Return<Type>& ret) {
if (ret.isDeadObject()) {
- return NN_ERROR(nn::ErrorStatus::DEAD_OBJECT)
+ return nn::error(nn::ErrorStatus::DEAD_OBJECT)
<< "Return<>::isDeadObject returned true: " << ret.description();
}
if (!ret.isOk()) {
- return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
+ return nn::error(nn::ErrorStatus::GENERAL_FAILURE)
<< "Return<>::isOk returned false: " << ret.description();
}
- return ret;
+ if constexpr (!std::is_same_v<Type, void>) {
+ return static_cast<Type>(ret);
+ } else {
+ return {};
+ }
}
-template <>
-inline nn::GeneralResult<void> handleTransportError(const hardware::Return<void>& ret) {
- if (ret.isDeadObject()) {
- return NN_ERROR(nn::ErrorStatus::DEAD_OBJECT)
- << "Return<>::isDeadObject returned true: " << ret.description();
- }
- if (!ret.isOk()) {
- return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
- << "Return<>::isOk returned false: " << ret.description();
- }
- return {};
-}
+#define HANDLE_TRANSPORT_FAILURE(ret) \
+ ({ \
+ auto result = ::android::hardware::neuralnetworks::utils::handleTransportError(ret); \
+ if (!result.has_value()) { \
+ return NN_ERROR(result.error().code) << result.error().message; \
+ } \
+ std::move(result).value(); \
+ })
template <typename Type>
nn::GeneralResult<Type> makeGeneralFailure(nn::Result<Type> result, nn::ErrorStatus status) {
if (!result.has_value()) {
return nn::error(status) << std::move(result).error();
}
- return std::move(result).value();
-}
-
-template <>
-inline nn::GeneralResult<void> makeGeneralFailure(nn::Result<void> result, nn::ErrorStatus status) {
- if (!result.has_value()) {
- return nn::error(status) << std::move(result).error();
+ if constexpr (!std::is_same_v<Type, void>) {
+ return std::move(result).value();
+ } else {
+ return {};
}
- return {};
-}
-
-template <typename Type>
-nn::ExecutionResult<Type> makeExecutionFailure(nn::Result<Type> result, nn::ErrorStatus status) {
- if (!result.has_value()) {
- return nn::error(status) << std::move(result).error();
- }
- return std::move(result).value();
-}
-
-template <>
-inline nn::ExecutionResult<void> makeExecutionFailure(nn::Result<void> result,
- nn::ErrorStatus status) {
- if (!result.has_value()) {
- return nn::error(status) << std::move(result).error();
- }
- return {};
}
template <typename Type>
@@ -86,16 +67,16 @@
const auto [message, status] = std::move(result).error();
return nn::error(status) << message;
}
- return std::move(result).value();
+ if constexpr (!std::is_same_v<Type, void>) {
+ return std::move(result).value();
+ } else {
+ return {};
+ }
}
-template <>
-inline nn::ExecutionResult<void> makeExecutionFailure(nn::GeneralResult<void> result) {
- if (!result.has_value()) {
- const auto [message, status] = std::move(result).error();
- return nn::error(status) << message;
- }
- return {};
+template <typename Type>
+nn::ExecutionResult<Type> makeExecutionFailure(nn::Result<Type> result, nn::ErrorStatus status) {
+ return makeExecutionFailure(makeGeneralFailure(result, status));
}
} // namespace android::hardware::neuralnetworks::utils
\ No newline at end of file
diff --git a/neuralnetworks/utils/common/src/ProtectCallback.cpp b/neuralnetworks/utils/common/src/ProtectCallback.cpp
index 1d9a307..abe4cb6 100644
--- a/neuralnetworks/utils/common/src/ProtectCallback.cpp
+++ b/neuralnetworks/utils/common/src/ProtectCallback.cpp
@@ -58,7 +58,7 @@
auto deathRecipient = sp<DeathRecipient>::make();
const auto ret = object->linkToDeath(deathRecipient, /*cookie=*/0);
- const bool success = NN_TRY(handleTransportError(ret));
+ const bool success = HANDLE_TRANSPORT_FAILURE(ret);
if (!success) {
return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "IBase::linkToDeath returned false";
}