weaved: Add Command::AbortWithCustomError() and SetError/SetCustomError
Client code often obtains error information through brillo::Error or
binder::Status during weave command execution. It is convenient to
use this error information directly to abort the weave command (or
put it into a temporary state).
Added helpers Command::AbortWithCustomError() as well as exposed
missing Command::SetError()/SetCustomError and Command::Pause() methods.
BUG: 25875613
Change-Id: Ic68871317b35604aa6bc5c4a20bb48c5c3153672
diff --git a/libweaved/command.cc b/libweaved/command.cc
index 9950673..fa10955 100644
--- a/libweaved/command.cc
+++ b/libweaved/command.cc
@@ -25,6 +25,34 @@
namespace weaved {
+namespace {
+
+// Converts binder exception code into a weave error code string.
+std::string BinderExceptionString(int32_t exception_code) {
+ if (exception_code == android::binder::Status::EX_NONE)
+ return "_none";
+ else if (exception_code == android::binder::Status::EX_SECURITY)
+ return "_security";
+ else if (exception_code == android::binder::Status::EX_BAD_PARCELABLE)
+ return "_bad_parcelable";
+ else if (exception_code == android::binder::Status::EX_ILLEGAL_ARGUMENT)
+ return "_illegal_argument";
+ else if (exception_code == android::binder::Status::EX_NULL_POINTER)
+ return "_null_pointer";
+ else if (exception_code == android::binder::Status::EX_ILLEGAL_STATE)
+ return "_illegal_state";
+ else if (exception_code == android::binder::Status::EX_NETWORK_MAIN_THREAD)
+ return "_network_error";
+ else if (exception_code == android::binder::Status::EX_UNSUPPORTED_OPERATION)
+ return "_unsupported_operation";
+ else if (exception_code == android::binder::Status::EX_SERVICE_SPECIFIC)
+ return "_general_failure";
+
+ return "_unknown";
+}
+
+} // anonymous namespace
+
Command::Command(const android::sp<android::weave::IWeaveCommand>& proxy)
: binder_proxy_{proxy} {}
@@ -125,8 +153,44 @@
error);
}
+bool Command::AbortWithCustomError(const brillo::Error* command_error,
+ brillo::ErrorPtr* error) {
+ std::string error_code = "_" + command_error->GetCode();
+ return Abort(error_code, command_error->GetMessage(), error);
+}
+
+bool Command::AbortWithCustomError(android::binder::Status status,
+ brillo::ErrorPtr* error) {
+ std::string error_code = BinderExceptionString(status.exceptionCode());
+ return Abort(error_code, status.exceptionMessage().string(), error);
+}
+
bool Command::Cancel(brillo::ErrorPtr* error) {
return StatusToError(binder_proxy_->cancel(), error);
}
+bool Command::Pause(brillo::ErrorPtr* error) {
+ return StatusToError(binder_proxy_->pause(), error);
+}
+
+bool Command::SetError(const std::string& error_code,
+ const std::string& error_message,
+ brillo::ErrorPtr* error) {
+ return StatusToError(binder_proxy_->setError(ToString16(error_code),
+ ToString16(error_message)),
+ error);
+}
+
+bool Command::SetCustomError(const brillo::Error* command_error,
+ brillo::ErrorPtr* error) {
+ std::string error_code = "_" + command_error->GetCode();
+ return SetError(error_code, command_error->GetMessage(), error);
+}
+
+bool Command::SetCustomError(android::binder::Status status,
+ brillo::ErrorPtr* error) {
+ std::string error_code = BinderExceptionString(status.exceptionCode());
+ return SetError(error_code, status.exceptionMessage().string(), error);
+}
+
} // namespace weave