diff --git a/src/error.rs b/src/error.rs index 5c66e190..0336f08c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -19,7 +19,7 @@ use std::result; use thiserror::Error; /// The error type for ttrpc. -#[derive(Error, Debug, Clone)] +#[derive(Error, Debug, Clone, PartialEq)] pub enum Error { #[error("socket err: {0}")] Socket(String), diff --git a/src/sync/sys/windows/net.rs b/src/sync/sys/windows/net.rs index 1a5adbca..3787d845 100644 --- a/src/sync/sys/windows/net.rs +++ b/src/sync/sys/windows/net.rs @@ -321,9 +321,14 @@ impl ClientConnection { opts.read(true) .write(true) .custom_flags(FILE_FLAG_OVERLAPPED); - let file = opts.open(self.address.as_str()); - - return PipeConnection::new(file.unwrap().into_raw_handle() as isize) + match opts.open(self.address.as_str()) { + Ok(file) => { + return PipeConnection::new(file.into_raw_handle() as isize) + } + Err(e) => { + return Err(Error::Windows(e.raw_os_error().unwrap())) + } + } } pub fn close_receiver(&self) -> Result<()> { @@ -336,3 +341,22 @@ impl ClientConnection { Ok(()) } } + +#[cfg(test)] +mod test { + use super::*; + use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND; + + #[test] + fn test_pipe_connection() { + let client = ClientConnection::new("non_existent_pipe"); + match client.get_pipe_connection() { + Ok(_) => { + assert!(false, "should not be able to get a connection to a non existent pipe"); + } + Err(e) => { + assert_eq!(e, Error::Windows(ERROR_FILE_NOT_FOUND as i32)); + } + } + } +}