Migrate errors to binder::IntoBinderResult
This removes some of the boilerplate and simplifies the code a little.
Most error handling was already centralized into a single helper, so
this isn't a big change.
Where I'm creating a new error, rather than reporting one from
elsewhere, I've used a straight String rather than anyhow!(); no need
for the extra functionality Anyhow provides.
Bug: 294348831
Test: composd_cmd test-compile
Test: Trigger binder error, see expected message
Change-Id: I68ebd5ce54131f84e7b0fd1af94fc1654702c5a5
diff --git a/compos/common/binder.rs b/compos/common/binder.rs
index d3550f7..aea0072 100644
--- a/compos/common/binder.rs
+++ b/compos/common/binder.rs
@@ -16,7 +16,7 @@
//! Helper for converting Error types to what Binder expects
-use binder::{Result as BinderResult, Status};
+use binder::{IntoBinderResult, Result as BinderResult};
use log::warn;
use std::fmt::Debug;
@@ -24,9 +24,9 @@
/// preserving the content as far as possible.
/// Also log the error if there is one.
pub fn to_binder_result<T, E: Debug>(result: Result<T, E>) -> BinderResult<T> {
- result.map_err(|e| {
+ result.or_service_specific_exception_with(-1, |e| {
let message = format!("{:?}", e);
- warn!("Returning binder error: {}", &message);
- Status::new_service_specific_error_str(-1, Some(message))
+ warn!("Returning binder error: {message}");
+ message
})
}
diff --git a/compos/src/compsvc.rs b/compos/src/compsvc.rs
index 8febd52..fe83ba2 100644
--- a/compos/src/compsvc.rs
+++ b/compos/src/compsvc.rs
@@ -33,7 +33,9 @@
use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::{
IAuthFsService, AUTHFS_SERVICE_SOCKET_NAME,
};
-use binder::{BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, Strong};
+use binder::{
+ BinderFeatures, ExceptionCode, Interface, IntoBinderResult, Result as BinderResult, Strong,
+};
use compos_aidl_interface::aidl::com::android::compos::ICompOsService::{
BnCompOsService, ICompOsService, OdrefreshArgs::OdrefreshArgs,
};
@@ -66,29 +68,23 @@
fn initializeSystemProperties(&self, names: &[String], values: &[String]) -> BinderResult<()> {
let mut initialized = self.initialized.write().unwrap();
if initialized.is_some() {
- return Err(Status::new_exception_str(
- ExceptionCode::ILLEGAL_STATE,
- Some(format!("Already initialized: {:?}", initialized)),
- ));
+ return Err(format!("Already initialized: {initialized:?}"))
+ .or_binder_exception(ExceptionCode::ILLEGAL_STATE);
}
*initialized = Some(false);
if names.len() != values.len() {
- return Err(Status::new_exception_str(
- ExceptionCode::ILLEGAL_ARGUMENT,
- Some(format!(
- "Received inconsistent number of keys ({}) and values ({})",
- names.len(),
- values.len()
- )),
- ));
+ return Err(format!(
+ "Received inconsistent number of keys ({}) and values ({})",
+ names.len(),
+ values.len()
+ ))
+ .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
}
for (name, value) in zip(names, values) {
if !is_system_property_interesting(name) {
- return Err(Status::new_exception_str(
- ExceptionCode::ILLEGAL_ARGUMENT,
- Some(format!("Received invalid system property {}", &name)),
- ));
+ return Err(format!("Received invalid system property {name}"))
+ .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
}
let result = system_properties::write(name, value);
if result.is_err() {
@@ -103,10 +99,8 @@
fn odrefresh(&self, args: &OdrefreshArgs) -> BinderResult<i8> {
let initialized = *self.initialized.read().unwrap();
if !initialized.unwrap_or(false) {
- return Err(Status::new_exception_str(
- ExceptionCode::ILLEGAL_STATE,
- Some("Service has not been initialized"),
- ));
+ return Err("Service has not been initialized")
+ .or_binder_exception(ExceptionCode::ILLEGAL_STATE);
}
to_binder_result(self.do_odrefresh(args))