Skip to content

Commit e7eab4f

Browse files
committed
Getting rid of all expect()'s
1 parent 88e92af commit e7eab4f

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/raw_client.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ impl<S: Read + Write> RawClient<S> {
464464
if let Err(e) = reader.read_line(&mut raw_resp) {
465465
let error = Arc::new(e);
466466
for (_, s) in self.waiting_map.lock().unwrap().drain() {
467-
s.send(ChannelMessage::Error(error.clone()))
468-
.expect("Unable to send ChannelMessage::Error");
467+
s.send(ChannelMessage::Error(error.clone()))?;
469468
}
470469
return Err(Error::SharedIOError(error));
471470
}
@@ -494,9 +493,7 @@ impl<S: Read + Write> RawClient<S> {
494493
// If the map is not empty, we select a random thread to become the
495494
// new reader thread.
496495
if let Some(sender) = map.values().next() {
497-
sender
498-
.send(ChannelMessage::WakeUp)
499-
.expect("Unable to WakeUp a different thread");
496+
sender.send(ChannelMessage::WakeUp)?;
500497
}
501498

502499
break Ok(resp);
@@ -507,9 +504,7 @@ impl<S: Read + Write> RawClient<S> {
507504
trace!("Reader thread received response for {}", resp_id);
508505

509506
if let Some(sender) = self.waiting_map.lock()?.remove(&resp_id) {
510-
sender
511-
.send(ChannelMessage::Response(resp))
512-
.expect("Unable to send the response");
507+
sender.send(ChannelMessage::Response(resp))?;
513508
} else {
514509
warn!("Missing listener for {}", resp_id);
515510
}
@@ -532,9 +527,7 @@ impl<S: Read + Write> RawClient<S> {
532527
// running somewhere.
533528
Err(Error::CouldntLockReader)
534529
}
535-
e @ Err(TryLockError::Poisoned(_)) => e
536-
.map(|_| Ok(serde_json::Value::Null))
537-
.expect("Poisoned reader mutex"), // panic if the reader mutex has been poisoned
530+
Err(TryLockError::Poisoned(e)) => Err(e)?,
538531
};
539532

540533
let resp = resp?;
@@ -585,22 +578,21 @@ impl<S: Read + Write> RawClient<S> {
585578
match self._reader_thread(Some(req_id)) {
586579
Ok(response) => break Ok(response),
587580
Err(Error::CouldntLockReader) => {
588-
match receiver.recv() {
581+
match receiver.recv()? {
589582
// Received our response, returning it
590-
Ok(ChannelMessage::Response(received)) => break Ok(received),
591-
Ok(ChannelMessage::WakeUp) => {
583+
ChannelMessage::Response(received) => break Ok(received),
584+
ChannelMessage::WakeUp => {
592585
// We have been woken up, this means that we should try becoming the
593586
// reader thread ourselves
594587
trace!("WakeUp for {}", req_id);
595588

596589
continue;
597590
}
598-
Ok(ChannelMessage::Error(e)) => {
591+
ChannelMessage::Error(e) => {
599592
warn!("Received ChannelMessage::Error");
600593

601594
break Err(Error::SharedIOError(e));
602595
}
603-
e @ Err(_) => e.map(|_| ()).expect("Error receiving from channel"), // panic if there's something wrong with the channels
604596
}
605597
}
606598
e @ Err(_) => break e,

src/types.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use bitcoin::hashes::{sha256, Hash};
1414
use bitcoin::{Script, Txid};
1515

1616
use serde::{de, Deserialize, Serialize};
17-
use std::sync::PoisonError;
1817

1918
static JSONRPC_2_0: &str = "2.0";
2019

@@ -301,6 +300,8 @@ pub enum Error {
301300
/// Couldn't take a lock on the reader mutex. This means that there's already another reader
302301
/// thread running
303302
CouldntLockReader,
303+
/// Broken IPC communication channel: the other thread probably has exited
304+
Mpsc,
304305

305306
#[cfg(feature = "use-openssl")]
306307
/// Invalid OpenSSL method used
@@ -365,7 +366,19 @@ impl_error!(bitcoin::hashes::hex::Error, Hex);
365366
impl_error!(bitcoin::consensus::encode::Error, Bitcoin);
366367

367368
impl<T> From<std::sync::PoisonError<T>> for Error {
368-
fn from(_: PoisonError<T>) -> Self {
369+
fn from(_: std::sync::PoisonError<T>) -> Self {
369370
Error::CouldntLockReader
370371
}
371372
}
373+
374+
impl<T> From<std::sync::mpsc::SendError<T>> for Error {
375+
fn from(_: std::sync::mpsc::SendError<T>) -> Self {
376+
Error::Mpsc
377+
}
378+
}
379+
380+
impl From<std::sync::mpsc::RecvError> for Error {
381+
fn from(_: std::sync::mpsc::RecvError) -> Self {
382+
Error::Mpsc
383+
}
384+
}

0 commit comments

Comments
 (0)