Use new binder exception constructors moved from binder_common.

Bug: 234019127
Test: atest compos_key_tests MicrodroidHostTestCases MicrodroidTestApp
Change-Id: I938c9d0ebae90c933c9a7ee8c27e9ecb0cc3e5fa
diff --git a/virtualizationservice/src/aidl.rs b/virtualizationservice/src/aidl.rs
index cc8d8a3..b46d5f8 100644
--- a/virtualizationservice/src/aidl.rs
+++ b/virtualizationservice/src/aidl.rs
@@ -45,7 +45,7 @@
     },
 };
 use anyhow::{anyhow, bail, Context, Result};
-use binder_common::{lazy_service::LazyServiceGuard, new_binder_exception, rpc_server::run_rpc_server_with_factory};
+use binder_common::{lazy_service::LazyServiceGuard, rpc_server::run_rpc_server_with_factory};
 use disk::QcowFile;
 use idsig::{HashAlgorithm, V4Signature};
 use log::{debug, error, info, warn, trace};
@@ -160,23 +160,23 @@
     ) -> binder::Result<()> {
         check_manage_access()?;
         let size = size.try_into().map_err(|e| {
-            new_binder_exception(
+            Status::new_exception_str(
                 ExceptionCode::ILLEGAL_ARGUMENT,
-                format!("Invalid size {}: {}", size, e),
+                Some(format!("Invalid size {}: {}", size, e)),
             )
         })?;
         let image = clone_file(image_fd)?;
         // initialize the file. Any data in the file will be erased.
         image.set_len(0).map_err(|e| {
-            new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("Failed to reset a file: {}", e),
+            Status::new_service_specific_error_str(
+                -1,
+                Some(format!("Failed to reset a file: {}", e)),
             )
         })?;
         let mut part = QcowFile::new(image, size).map_err(|e| {
-            new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("Failed to create QCOW2 image: {}", e),
+            Status::new_service_specific_error_str(
+                -1,
+                Some(format!("Failed to create QCOW2 image: {}", e)),
             )
         })?;
 
@@ -189,9 +189,9 @@
             )),
         }
         .map_err(|e| {
-            new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("Failed to initialize partition as {:?}: {}", partition_type, e),
+            Status::new_service_specific_error_str(
+                -1,
+                Some(format!("Failed to initialize partition as {:?}: {}", partition_type, e)),
             )
         })?;
 
@@ -379,12 +379,12 @@
                 "Failed to create temporary directory {:?} for VM files: {:?}",
                 temporary_directory, e
             );
-            new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!(
+            Status::new_service_specific_error_str(
+                -1,
+                Some(format!(
                     "Failed to create temporary directory {:?} for VM files: {}",
                     temporary_directory, e
-                ),
+                )),
             )
         })?;
 
@@ -395,9 +395,12 @@
                 load_app_config(config, &temporary_directory).map_err(|e| {
                     error!("Failed to load app config from {}: {:?}", &config.configPath, e);
                     *is_protected = config.protectedVm;
-                    new_binder_exception(
-                        ExceptionCode::SERVICE_SPECIFIC,
-                        format!("Failed to load app config from {}: {}", &config.configPath, e),
+                    Status::new_service_specific_error_str(
+                        -1,
+                        Some(format!(
+                            "Failed to load app config from {}: {}",
+                            &config.configPath, e
+                        )),
                     )
                 })?,
             ),
@@ -423,14 +426,14 @@
                 }
             })
             .try_for_each(check_label_for_partition)
-            .map_err(|e| new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string()))?;
+            .map_err(|e| Status::new_service_specific_error_str(-1, Some(e.to_string())))?;
 
         let zero_filler_path = temporary_directory.join("zero.img");
         write_zero_filler(&zero_filler_path).map_err(|e| {
             error!("Failed to make composite image: {:?}", e);
-            new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("Failed to make composite image: {}", e),
+            Status::new_service_specific_error_str(
+                -1,
+                Some(format!("Failed to make composite image: {}", e)),
             )
         })?;
 
@@ -456,9 +459,9 @@
         let ramdump_path = temporary_directory.join("ramdump");
         let ramdump = prepare_ramdump_file(&ramdump_path).map_err(|e| {
             error!("Failed to prepare ramdump file: {}", e);
-            new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("Failed to prepare ramdump file: {}", e),
+            Status::new_service_specific_error_str(
+                -1,
+                Some(format!("Failed to prepare ramdump file: {}", e)),
             )
         })?;
 
@@ -492,9 +495,9 @@
             )
             .map_err(|e| {
                 error!("Failed to create VM with config {:?}: {:?}", config, e);
-                new_binder_exception(
-                    ExceptionCode::SERVICE_SPECIFIC,
-                    format!("Failed to create VM: {}", e),
+                Status::new_service_specific_error_str(
+                    -1,
+                    Some(format!("Failed to create VM: {}", e)),
                 )
             })?,
         );
@@ -575,9 +578,9 @@
     let image = if !disk.partitions.is_empty() {
         if disk.image.is_some() {
             warn!("DiskImage {:?} contains both image and partitions.", disk);
-            return Err(new_binder_exception(
+            return Err(Status::new_exception_str(
                 ExceptionCode::ILLEGAL_ARGUMENT,
-                "DiskImage contains both image and partitions.",
+                Some("DiskImage contains both image and partitions."),
             ));
         }
 
@@ -592,9 +595,9 @@
         )
         .map_err(|e| {
             error!("Failed to make composite image with config {:?}: {:?}", disk, e);
-            new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("Failed to make composite image: {}", e),
+            Status::new_service_specific_error_str(
+                -1,
+                Some(format!("Failed to make composite image: {}", e)),
             )
         })?;
 
@@ -607,9 +610,9 @@
         clone_file(image)?
     } else {
         warn!("DiskImage {:?} didn't contain image or partitions.", disk);
-        return Err(new_binder_exception(
+        return Err(Status::new_exception_str(
             ExceptionCode::ILLEGAL_ARGUMENT,
-            "DiskImage didn't contain image or partitions.",
+            Some("DiskImage didn't contain image or partitions."),
         ));
     };
 
@@ -700,15 +703,15 @@
                 Ok(sid) => Ok(sid.to_owned()),
                 Err(e) => {
                     error!("SID was not valid UTF-8: {}", e);
-                    Err(new_binder_exception(
+                    Err(Status::new_exception_str(
                         ExceptionCode::ILLEGAL_ARGUMENT,
-                        format!("SID was not valid UTF-8: {}", e),
+                        Some(format!("SID was not valid UTF-8: {}", e)),
                     ))
                 }
             }
         } else {
             error!("Missing SID on createVm");
-            Err(new_binder_exception(ExceptionCode::SECURITY, "Missing SID on createVm"))
+            Err(Status::new_exception_str(ExceptionCode::SECURITY, Some("Missing SID on createVm")))
         }
     })
 }
@@ -726,9 +729,9 @@
     if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? {
         Ok(())
     } else {
-        Err(new_binder_exception(
+        Err(Status::new_exception_str(
             ExceptionCode::SECURITY,
-            format!("does not have the {} permission", perm),
+            Some(format!("does not have the {} permission", perm)),
         ))
     }
 }
@@ -804,26 +807,26 @@
     fn start(&self) -> binder::Result<()> {
         self.instance.start().map_err(|e| {
             error!("Error starting VM with CID {}: {:?}", self.instance.cid, e);
-            new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string())
+            Status::new_service_specific_error_str(-1, Some(e.to_string()))
         })
     }
 
     fn stop(&self) -> binder::Result<()> {
         self.instance.kill().map_err(|e| {
             error!("Error stopping VM with CID {}: {:?}", self.instance.cid, e);
-            new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string())
+            Status::new_service_specific_error_str(-1, Some(e.to_string()))
         })
     }
 
     fn connectVsock(&self, port: i32) -> binder::Result<ParcelFileDescriptor> {
         if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) {
-            return Err(new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, "VM is not running"));
+            return Err(Status::new_service_specific_error_str(-1, Some("VM is not running")));
         }
         let stream =
             VsockStream::connect_with_cid_port(self.instance.cid, port as u32).map_err(|e| {
-                new_binder_exception(
-                    ExceptionCode::SERVICE_SPECIFIC,
-                    format!("Failed to connect: {}", e),
+                Status::new_service_specific_error_str(
+                    -1,
+                    Some(format!("Failed to connect: {}", e)),
                 )
             })?;
         Ok(vsock_stream_to_pfd(stream))
@@ -1002,9 +1005,9 @@
 /// Converts a `&ParcelFileDescriptor` to a `File` by cloning the file.
 fn clone_file(file: &ParcelFileDescriptor) -> Result<File, Status> {
     file.as_ref().try_clone().map_err(|e| {
-        new_binder_exception(
+        Status::new_exception_str(
             ExceptionCode::BAD_PARCELABLE,
-            format!("Failed to clone File from ParcelFileDescriptor: {}", e),
+            Some(format!("Failed to clone File from ParcelFileDescriptor: {}", e)),
         )
     })
 }
@@ -1024,9 +1027,9 @@
 /// Parses the platform version requirement string.
 fn parse_platform_version_req(s: &str) -> Result<VersionReq, Status> {
     VersionReq::parse(s).map_err(|e| {
-        new_binder_exception(
+        Status::new_exception_str(
             ExceptionCode::BAD_PARCELABLE,
-            format!("Invalid platform version requirement {}: {}", s, e),
+            Some(format!("Invalid platform version requirement {}: {}", s, e)),
         )
     })
 }
@@ -1061,16 +1064,17 @@
         let cid = self.cid;
         if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
             info!("VM having CID {} started payload", cid);
-            vm.update_payload_state(PayloadState::Started)
-                .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?;
+            vm.update_payload_state(PayloadState::Started).map_err(|e| {
+                Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
+            })?;
             let stream = vm.stream.lock().unwrap().take();
             vm.callbacks.notify_payload_started(cid, stream);
             Ok(())
         } else {
             error!("notifyPayloadStarted is called from an unknown CID {}", cid);
-            Err(new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("cannot find a VM with CID {}", cid),
+            Err(Status::new_service_specific_error_str(
+                -1,
+                Some(format!("cannot find a VM with CID {}", cid)),
             ))
         }
     }
@@ -1079,15 +1083,16 @@
         let cid = self.cid;
         if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
             info!("VM having CID {} payload is ready", cid);
-            vm.update_payload_state(PayloadState::Ready)
-                .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?;
+            vm.update_payload_state(PayloadState::Ready).map_err(|e| {
+                Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
+            })?;
             vm.callbacks.notify_payload_ready(cid);
             Ok(())
         } else {
             error!("notifyPayloadReady is called from an unknown CID {}", cid);
-            Err(new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("cannot find a VM with CID {}", cid),
+            Err(Status::new_service_specific_error_str(
+                -1,
+                Some(format!("cannot find a VM with CID {}", cid)),
             ))
         }
     }
@@ -1096,15 +1101,16 @@
         let cid = self.cid;
         if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
             info!("VM having CID {} finished payload", cid);
-            vm.update_payload_state(PayloadState::Finished)
-                .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?;
+            vm.update_payload_state(PayloadState::Finished).map_err(|e| {
+                Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
+            })?;
             vm.callbacks.notify_payload_finished(cid, exit_code);
             Ok(())
         } else {
             error!("notifyPayloadFinished is called from an unknown CID {}", cid);
-            Err(new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("cannot find a VM with CID {}", cid),
+            Err(Status::new_service_specific_error_str(
+                -1,
+                Some(format!("cannot find a VM with CID {}", cid)),
             ))
         }
     }
@@ -1113,15 +1119,16 @@
         let cid = self.cid;
         if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
             info!("VM having CID {} encountered an error", cid);
-            vm.update_payload_state(PayloadState::Finished)
-                .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?;
+            vm.update_payload_state(PayloadState::Finished).map_err(|e| {
+                Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
+            })?;
             vm.callbacks.notify_error(cid, error_code, message);
             Ok(())
         } else {
             error!("notifyError is called from an unknown CID {}", cid);
-            Err(new_binder_exception(
-                ExceptionCode::SERVICE_SPECIFIC,
-                format!("cannot find a VM with CID {}", cid),
+            Err(Status::new_service_specific_error_str(
+                -1,
+                Some(format!("cannot find a VM with CID {}", cid)),
             ))
         }
     }