Skip to content

Commit 57b2713

Browse files
committed
Unwrap error as windows error or otherwise return error message
When using raw_os_error it will return Some() if it is an OS error otherwise it will return None and the error isn't related to OS failure. This handles it there won't be a panic in the case of a non OS error. Signed-off-by: James Sturtevant <[email protected]>
1 parent 2396cf3 commit 57b2713

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/sync/sys/windows/net.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl PipeConnection {
213213
let res = unsafe {GetOverlappedResult(self.named_pipe, ol.as_mut_ptr(), &mut bytes_transfered, WAIT_FOR_EVENT) };
214214
match res {
215215
0 => {
216-
return Err(Error::Windows(io::Error::last_os_error().raw_os_error().unwrap()))
216+
return Err(handle_windows_error(io::Error::last_os_error()))
217217
}
218218
_ => {
219219
return Ok(bytes_transfered as usize)
@@ -244,7 +244,7 @@ impl PipeConnection {
244244
let res = unsafe {GetOverlappedResult(self.named_pipe, ol.as_mut_ptr(), &mut bytes_transfered, WAIT_FOR_EVENT) };
245245
match res {
246246
0 => {
247-
return Err(Error::Windows(io::Error::last_os_error().raw_os_error().unwrap()))
247+
return Err(handle_windows_error(io::Error::last_os_error()))
248248
}
249249
_ => {
250250
return Ok(bytes_transfered as usize)
@@ -266,7 +266,7 @@ impl PipeConnection {
266266
pub fn shutdown(&self) -> Result<()> {
267267
let result = unsafe { DisconnectNamedPipe(self.named_pipe) };
268268
match result {
269-
0 => Err(Error::Windows(io::Error::last_os_error().raw_os_error().unwrap())),
269+
0 => Err(handle_windows_error(io::Error::last_os_error())),
270270
_ => Ok(()),
271271
}
272272
}
@@ -279,23 +279,23 @@ pub struct ClientConnection {
279279
fn close_handle(handle: isize) -> Result<()> {
280280
let result = unsafe { CloseHandle(handle) };
281281
match result {
282-
0 => Err(Error::Windows(io::Error::last_os_error().raw_os_error().unwrap())),
282+
0 => Err(handle_windows_error(io::Error::last_os_error())),
283283
_ => Ok(()),
284284
}
285285
}
286286

287287
fn create_event() -> Result<isize> {
288288
let result = unsafe { CreateEventW(std::ptr::null_mut(), 0, 1, std::ptr::null_mut()) };
289289
match result {
290-
0 => Err(Error::Windows(io::Error::last_os_error().raw_os_error().unwrap())),
290+
0 => Err(handle_windows_error(io::Error::last_os_error())),
291291
_ => Ok(result),
292292
}
293293
}
294294

295295
fn set_event(event: isize) -> Result<()> {
296296
let result = unsafe { SetEvent(event) };
297297
match result {
298-
0 => Err(Error::Windows(io::Error::last_os_error().raw_os_error().unwrap())),
298+
0 => Err(handle_windows_error(io::Error::last_os_error())),
299299
_ => Ok(()),
300300
}
301301
}
@@ -326,7 +326,7 @@ impl ClientConnection {
326326
return PipeConnection::new(file.into_raw_handle() as isize)
327327
}
328328
Err(e) => {
329-
return Err(Error::Windows(e.raw_os_error().unwrap()))
329+
return Err(handle_windows_error(e))
330330
}
331331
}
332332
}
@@ -342,6 +342,14 @@ impl ClientConnection {
342342
}
343343
}
344344

345+
fn handle_windows_error(e: io::Error) -> Error {
346+
if let Some(raw_os_err) = e.raw_os_error() {
347+
Error::Windows(raw_os_err)
348+
} else {
349+
Error::Others(e.to_string())
350+
}
351+
}
352+
345353
#[cfg(test)]
346354
mod test {
347355
use super::*;

0 commit comments

Comments
 (0)