diff options
author | Jesse Luehrs <doy@tozt.net> | 2014-09-09 18:34:18 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2014-09-09 18:34:18 -0400 |
commit | f0bf003b94c723c27f9d2ece53cf8ef93bb56d28 (patch) | |
tree | 6596b14e4abf3cefd94957746381dd4ada550ef9 /src | |
parent | 41ff1965fa4270a3839b35cb3a39165d23e328aa (diff) | |
download | rust-irc-f0bf003b94c723c27f9d2ece53cf8ef93bb56d28.tar.gz rust-irc-f0bf003b94c723c27f9d2ece53cf8ef93bb56d28.zip |
more error handling
Diffstat (limited to 'src')
-rw-r--r-- | src/client.rs | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/client.rs b/src/client.rs index bddeb47..c4b507f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,4 @@ -use std::{io, str}; +use std::io; use constants::{ Pass, @@ -46,6 +46,12 @@ use constants::{ }; use message::Message; +pub enum MessageError { + ParseError(&'static str), + IoError(io::IoError), +} +pub type MessageResult = Result<Message, MessageError>; + pub struct ClientBuilder { nick: String, pass: Option<String>, @@ -132,14 +138,20 @@ impl Client { } } - pub fn read (&mut self) -> Message { + pub fn read (&mut self) -> MessageResult { // \n isn't valid inside a message, so this should be fine. if the \n // we find isn't preceded by a \r, this will be caught by the message // parser. - let buf = self.conn().read_until(b'\n'); + let buf = match self.conn().read_until(b'\n') { + Ok(b) => b, + Err(e) => return Err(IoError(e)), + }; + // XXX handle different encodings - // XXX proper error handling - Message::parse(str::from_utf8(buf.unwrap().as_slice()).unwrap()).unwrap() + match Message::parse(String::from_utf8_lossy(buf.as_slice()).as_slice()) { + Ok(m) => Ok(m), + Err(s) => Err(ParseError(s)), + } } pub fn write (&mut self, msg: Message) -> io::IoResult<()> { @@ -149,7 +161,15 @@ impl Client { pub fn run_loop (&mut self, handler: |&mut Client, &Message| -> io::IoResult<()>) -> io::IoError { loop { - let m = self.read(); + let m = match self.read() { + Ok(m) => m, + Err(ParseError(_e)) => { + // XXX this shouldn't stop the loop, but it's not clear + // what it should do - warn maybe? + continue + }, + Err(IoError(e)) => return e, + }; match handler(self, &m) { Err(e) => return e, _ => {}, |