From a8cdf1ecfce294bc7dda899d16aa4d9ffa5fafb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Em=C4=ABls=20Pi=C5=86=C4=B7is?= Date: Mon, 12 Feb 2018 19:16:00 +0000 Subject: [PATCH] Add error type for when there are no valid nicks --- src/client/mod.rs | 13 +++++++++---- src/error.rs | 4 ++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index a555767..df1fc6d 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -398,7 +398,7 @@ impl ClientState { let alt_nicks = self.config().alternate_nicknames(); let mut index = self.alt_nick_index.write().unwrap(); if *index >= alt_nicks.len() { - panic!("All specified nicknames were in use or disallowed.") + return Err(error::IrcError::NoUsableNick); } else { self.send(NICK(alt_nicks[*index].to_owned()))?; *index += 1; @@ -1044,7 +1044,6 @@ mod test { } #[test] - #[should_panic(expected = "All specified nicknames were in use or disallowed.")] fn ran_out_of_nicknames() { let value = ":irc.pdgn.co 433 * test :Nickname is already in use.\r\n\ :irc.pdgn.co 433 * test2 :Nickname is already in use.\r\n"; @@ -1052,9 +1051,15 @@ mod test { mock_initial_value: Some(value.to_owned()), ..test_config() }).unwrap(); - client.for_each_incoming(|message| { + let res = client.for_each_incoming(|message| { println!("{:?}", message); - }).unwrap(); + }); + use error::IrcError; + match res.expect_err("returned no error when no valid nicks were specified") { + IrcError::NoUsableNick => (), + _ => panic!("expected {} error when no valid nicks are specified"), + + } } #[test] diff --git a/src/error.rs b/src/error.rs index 47c0f1a..e2cedbc 100644 --- a/src/error.rs +++ b/src/error.rs @@ -91,6 +91,10 @@ pub enum IrcError { /// The data that failed to encode or decode. data: String, }, + + /// All specified nicks were in use or unusable. + #[fail(display = "no usable nicks")] + NoUsableNick } /// Errors that occur when parsing messages.