Merge pull request #14 from TyOverby/better-errors

More robust and precise error handling.
This commit is contained in:
Ty 2015-01-07 22:05:12 -08:00
commit cb0ef826f9
3 changed files with 156 additions and 96 deletions

View File

@ -13,7 +13,7 @@ use rustc_serialize::Encodable;
use rustc_serialize::Decodable; use rustc_serialize::Decodable;
pub use writer::EncoderWriter; pub use writer::EncoderWriter;
pub use reader::DecoderReader; pub use reader::{DecoderReader, DecodingResult, DecodingError};
mod writer; mod writer;
mod reader; mod reader;
@ -32,7 +32,7 @@ pub fn encode<T: Encodable>(t: &T, size_limit: SizeLimit) -> IoResult<Vec<u8>> {
} }
} }
pub fn decode<T: Decodable>(b: Vec<u8>, size_limit: SizeLimit) -> IoResult<T> { pub fn decode<T: Decodable>(b: Vec<u8>, size_limit: SizeLimit) -> DecodingResult<T> {
decode_from(&mut MemReader::new(b), size_limit) decode_from(&mut MemReader::new(b), size_limit)
} }
@ -40,7 +40,7 @@ pub fn encode_into<T: Encodable, W: Writer>(t: &T, w: &mut W, size_limit: SizeLi
t.encode(&mut writer::EncoderWriter::new(w, size_limit)) t.encode(&mut writer::EncoderWriter::new(w, size_limit))
} }
pub fn decode_from<R: Reader+Buffer, T: Decodable>(r: &mut R, size_limit: SizeLimit) -> IoResult<T> { pub fn decode_from<R: Reader+Buffer, T: Decodable>(r: &mut R, size_limit: SizeLimit) -> DecodingResult<T> {
Decodable::decode(&mut reader::DecoderReader::new(r, size_limit)) Decodable::decode(&mut reader::DecoderReader::new(r, size_limit))
} }

View File

@ -1,10 +1,46 @@
use std::io::{Buffer, Reader, IoError, IoResult, OtherIoError}; use std::io::{Buffer, Reader, IoError};
use std::error::Error; use std::error::{Error, FromError};
use rustc_serialize::Decoder; use rustc_serialize::Decoder;
use super::SizeLimit; use super::SizeLimit;
#[derive(PartialEq, Clone, Show)]
pub struct InvalidBytes {
desc: &'static str,
detail: Option<String>,
}
#[derive(PartialEq, Clone, Show)]
pub enum DecodingError {
IoError(IoError),
InvalidBytes(InvalidBytes),
}
pub type DecodingResult<T> = Result<T, DecodingError>;
impl Error for DecodingError {
fn description(&self) -> &str {
match *self {
DecodingError::IoError(ref err) => err.description(),
DecodingError::InvalidBytes(ref ib) => ib.desc
}
}
fn detail(&self) -> Option<String> {
match *self {
DecodingError::IoError(ref err) => err.detail(),
DecodingError::InvalidBytes(ref ib) => ib.detail.clone()
}
}
}
impl FromError<IoError> for DecodingError {
fn from_error(err: IoError) -> DecodingError {
DecodingError::IoError(err)
}
}
pub struct DecoderReader<'a, R: 'a> { pub struct DecoderReader<'a, R: 'a> {
reader: &'a mut R, reader: &'a mut R,
size_limit: SizeLimit size_limit: SizeLimit
@ -20,161 +56,171 @@ impl<'a, R: Reader+Buffer> DecoderReader<'a, R> {
} }
impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> { impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
type Error = IoError; type Error = DecodingError;
fn read_nil(&mut self) -> IoResult<()> { fn read_nil(&mut self) -> DecodingResult<()> {
Ok(()) Ok(())
} }
fn read_uint(&mut self) -> IoResult<uint> { fn read_uint(&mut self) -> DecodingResult<uint> {
self.read_u64().map(|x| x as uint) Ok(try!(self.read_u64().map(|x| x as uint)))
} }
fn read_u64(&mut self) -> IoResult<u64> { fn read_u64(&mut self) -> DecodingResult<u64> {
self.reader.read_be_u64() Ok(try!(self.reader.read_be_u64()))
} }
fn read_u32(&mut self) -> IoResult<u32> { fn read_u32(&mut self) -> DecodingResult<u32> {
self.reader.read_be_u32() Ok(try!(self.reader.read_be_u32()))
} }
fn read_u16(&mut self) -> IoResult<u16> { fn read_u16(&mut self) -> DecodingResult<u16> {
self.reader.read_be_u16() Ok(try!(self.reader.read_be_u16()))
} }
fn read_u8(&mut self) -> IoResult<u8> { fn read_u8(&mut self) -> DecodingResult<u8> {
self.reader.read_u8() Ok(try!(self.reader.read_u8()))
} }
fn read_int(&mut self) -> IoResult<int> { fn read_int(&mut self) -> DecodingResult<int> {
self.read_i64().map(|x| x as int) Ok(try!(self.read_i64().map(|x| x as int)))
} }
fn read_i64(&mut self) -> IoResult<i64> { fn read_i64(&mut self) -> DecodingResult<i64> {
self.reader.read_be_i64() Ok(try!(self.reader.read_be_i64()))
} }
fn read_i32(&mut self) -> IoResult<i32> { fn read_i32(&mut self) -> DecodingResult<i32> {
self.reader.read_be_i32() Ok(try!(self.reader.read_be_i32()))
} }
fn read_i16(&mut self) -> IoResult<i16> { fn read_i16(&mut self) -> DecodingResult<i16> {
self.reader.read_be_i16() Ok(try!(self.reader.read_be_i16()))
} }
fn read_i8(&mut self) -> IoResult<i8> { fn read_i8(&mut self) -> DecodingResult<i8> {
self.reader.read_i8() Ok(try!(self.reader.read_i8()))
} }
fn read_bool(&mut self) -> IoResult<bool> { fn read_bool(&mut self) -> DecodingResult<bool> {
match try!(self.reader.read_i8()) { let x = try!(self.reader.read_u8());
match x {
1 => Ok(true), 1 => Ok(true),
_ => Ok(false) 0 => Ok(false),
_ => Err(DecodingError::InvalidBytes(InvalidBytes{
desc: "invalid u8 when decoding bool",
detail: Some(format!("Expected 0 or 1, got {}", x))
})),
} }
} }
fn read_f64(&mut self) -> IoResult<f64> { fn read_f64(&mut self) -> DecodingResult<f64> {
self.reader.read_be_f64() Ok(try!(self.reader.read_be_f64()))
} }
fn read_f32(&mut self) -> IoResult<f32> { fn read_f32(&mut self) -> DecodingResult<f32> {
self.reader.read_be_f32() Ok(try!(self.reader.read_be_f32()))
} }
fn read_char(&mut self) -> IoResult<char> { fn read_char(&mut self) -> DecodingResult<char> {
self.reader.read_char() Ok(try!(self.reader.read_char()))
} }
fn read_str(&mut self) -> IoResult<String> { fn read_str(&mut self) -> DecodingResult<String> {
let len = try!(self.read_uint()); let len = try!(self.read_uint());
let vector = try!(self.reader.read_exact(len)); let vector = try!(self.reader.read_exact(len));
String::from_utf8(vector).map_err(|e| IoError { match String::from_utf8(vector) {
kind: OtherIoError, Ok(s) => Ok(s),
desc: "invalid utf-8", Err(err) => Err(DecodingError::InvalidBytes(InvalidBytes {
detail: e.detail() desc: "error while decoding utf8 string",
}) detail: Some(format!("Decoding error: {}", err))
})),
} }
fn read_enum<T, F>(&mut self, _: &str, f: F) -> IoResult<T> where }
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { fn read_enum<T, F>(&mut self, _: &str, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> IoResult<T> where fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodingResult<T> where
F: FnMut(&mut DecoderReader<'a, R>, uint) -> IoResult<T> { F: FnMut(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
let id = try!(self.read_u32()); let id = try!(self.read_u32());
let id = id as uint; let id = id as uint;
if id >= names.len() { if id >= names.len() {
Err(IoError { Err(DecodingError::InvalidBytes(InvalidBytes {
kind: OtherIoError,
desc: "out of bounds tag when reading enum variant", desc: "out of bounds tag when reading enum variant",
detail: Some(format!("Expected tag < {}, got {}", names.len(), id)) detail: Some(format!("Expected tag < {}, got {}", names.len(), id))
}) }))
} else { } else {
f(self, id) f(self, id)
} }
} }
fn read_enum_variant_arg<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where fn read_enum_variant_arg<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> IoResult<T> where fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodingResult<T> where
F: FnMut(&mut DecoderReader<'a, R>, uint) -> IoResult<T> { F: FnMut(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
self.read_enum_variant(names, f) self.read_enum_variant(names, f)
} }
fn read_enum_struct_variant_field<T, F>(&mut self, fn read_enum_struct_variant_field<T, F>(&mut self,
_: &str, _: &str,
f_idx: uint, f_idx: uint,
f: F) f: F)
-> IoResult<T> where -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
self.read_enum_variant_arg(f_idx, f) self.read_enum_variant_arg(f_idx, f)
} }
fn read_struct<T, F>(&mut self, _: &str, _: uint, f: F) -> IoResult<T> where fn read_struct<T, F>(&mut self, _: &str, _: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn read_struct_field<T, F>(&mut self, fn read_struct_field<T, F>(&mut self,
_: &str, _: &str,
_: uint, _: uint,
f: F) f: F)
-> IoResult<T> where -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn read_tuple<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where fn read_tuple<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn read_tuple_arg<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where fn read_tuple_arg<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn read_tuple_struct<T, F>(&mut self, _: &str, len: uint, f: F) -> IoResult<T> where fn read_tuple_struct<T, F>(&mut self, _: &str, len: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
self.read_tuple(len, f) self.read_tuple(len, f)
} }
fn read_tuple_struct_arg<T, F>(&mut self, a_idx: uint, f: F) -> IoResult<T> where fn read_tuple_struct_arg<T, F>(&mut self, a_idx: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
self.read_tuple_arg(a_idx, f) self.read_tuple_arg(a_idx, f)
} }
fn read_option<T, F>(&mut self, mut f: F) -> IoResult<T> where fn read_option<T, F>(&mut self, mut f: F) -> DecodingResult<T> where
F: FnMut(&mut DecoderReader<'a, R>, bool) -> IoResult<T> { F: FnMut(&mut DecoderReader<'a, R>, bool) -> DecodingResult<T> {
match try!(self.reader.read_u8()) { let x = try!(self.reader.read_u8());
match x {
1 => f(self, true), 1 => f(self, true),
_ => f(self, false) 0 => f(self, false),
_ => Err(DecodingError::InvalidBytes(InvalidBytes {
desc: "invalid tag when decoding Option",
detail: Some(format!("Expected 0 or 1, got {}", x))
})),
} }
} }
fn read_seq<T, F>(&mut self, f: F) -> IoResult<T> where fn read_seq<T, F>(&mut self, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>, uint) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
let len = try!(self.read_uint()); let len = try!(self.read_uint());
f(self, len) f(self, len)
} }
fn read_seq_elt<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where fn read_seq_elt<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn read_map<T, F>(&mut self, f: F) -> IoResult<T> where fn read_map<T, F>(&mut self, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>, uint) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
let len = try!(self.read_uint()); let len = try!(self.read_uint());
f(self, len) f(self, len)
} }
fn read_map_elt_key<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where fn read_map_elt_key<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn read_map_elt_val<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where fn read_map_elt_val<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
f(self) f(self)
} }
fn error(&mut self, err: &str) -> IoError { fn error(&mut self, err: &str) -> DecodingError {
IoError { DecodingError::InvalidBytes(InvalidBytes {
kind: OtherIoError, desc: "user-induced error",
desc: "failure decoding or something, I don't know", detail: Some(err.to_string()),
detail: Some(err.to_string()) })
}
} }
} }

View File

@ -13,6 +13,8 @@ use rustc_serialize::{
use super::{ use super::{
encode, encode,
decode, decode,
DecodingError,
DecodingResult
}; };
use super::SizeLimit::Infinite; use super::SizeLimit::Infinite;
@ -49,7 +51,6 @@ fn test_numbers() {
fn test_string() { fn test_string() {
the_same("".to_string()); the_same("".to_string());
the_same("a".to_string()); the_same("a".to_string());
the_same("ƒoo".to_string());
} }
#[test] #[test]
@ -166,12 +167,25 @@ fn unicode() {
the_same("aåååååååa".to_string()); the_same("aåååååååa".to_string());
} }
#[test] #[cfg(test)]
fn bad_unicode() { fn is_invalid_bytes<T>(res: DecodingResult<T>) {
// This is a malformed message that contains bad utf8. match res {
// The decoding should return Err but not panic. Ok(_) => panic!("Expecting error"),
let encoded = vec![0,0,0,0, 0,0,0,2, 97, 195]; Err(DecodingError::IoError(_)) => panic!("Expecting InvalidBytes"),
let decoded: Result<String, _> = decode(encoded, Infinite); Err(DecodingError::InvalidBytes(_)) => {},
}
assert!(decoded.is_err()); }
#[test]
fn decoding_errors() {
is_invalid_bytes(decode::<bool>(vec![0xA], Infinite));
is_invalid_bytes(decode::<String>(vec![0, 0, 0, 0, 0, 0, 0, 1, 0xFF], Infinite));
// Out-of-bounds variant
#[derive(RustcEncodable, RustcDecodable)]
enum Test {
One,
Two,
};
is_invalid_bytes(decode::<Test>(vec![0, 0, 0, 5], Infinite));
is_invalid_bytes(decode::<Option<u8>>(vec![5, 0], Infinite));
} }