Merge pull request #14 from TyOverby/better-errors
More robust and precise error handling.
This commit is contained in:
commit
cb0ef826f9
|
|
@ -13,7 +13,7 @@ use rustc_serialize::Encodable;
|
|||
use rustc_serialize::Decodable;
|
||||
|
||||
pub use writer::EncoderWriter;
|
||||
pub use reader::DecoderReader;
|
||||
pub use reader::{DecoderReader, DecodingResult, DecodingError};
|
||||
|
||||
mod writer;
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
@ -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))
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
|
|
|
|||
214
src/reader.rs
214
src/reader.rs
|
|
@ -1,10 +1,46 @@
|
|||
use std::io::{Buffer, Reader, IoError, IoResult, OtherIoError};
|
||||
use std::error::Error;
|
||||
use std::io::{Buffer, Reader, IoError};
|
||||
use std::error::{Error, FromError};
|
||||
|
||||
use rustc_serialize::Decoder;
|
||||
|
||||
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> {
|
||||
reader: &'a mut R,
|
||||
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> {
|
||||
type Error = IoError;
|
||||
type Error = DecodingError;
|
||||
|
||||
fn read_nil(&mut self) -> IoResult<()> {
|
||||
fn read_nil(&mut self) -> DecodingResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
fn read_uint(&mut self) -> IoResult<uint> {
|
||||
self.read_u64().map(|x| x as uint)
|
||||
fn read_uint(&mut self) -> DecodingResult<uint> {
|
||||
Ok(try!(self.read_u64().map(|x| x as uint)))
|
||||
}
|
||||
fn read_u64(&mut self) -> IoResult<u64> {
|
||||
self.reader.read_be_u64()
|
||||
fn read_u64(&mut self) -> DecodingResult<u64> {
|
||||
Ok(try!(self.reader.read_be_u64()))
|
||||
}
|
||||
fn read_u32(&mut self) -> IoResult<u32> {
|
||||
self.reader.read_be_u32()
|
||||
fn read_u32(&mut self) -> DecodingResult<u32> {
|
||||
Ok(try!(self.reader.read_be_u32()))
|
||||
}
|
||||
fn read_u16(&mut self) -> IoResult<u16> {
|
||||
self.reader.read_be_u16()
|
||||
fn read_u16(&mut self) -> DecodingResult<u16> {
|
||||
Ok(try!(self.reader.read_be_u16()))
|
||||
}
|
||||
fn read_u8(&mut self) -> IoResult<u8> {
|
||||
self.reader.read_u8()
|
||||
fn read_u8(&mut self) -> DecodingResult<u8> {
|
||||
Ok(try!(self.reader.read_u8()))
|
||||
}
|
||||
fn read_int(&mut self) -> IoResult<int> {
|
||||
self.read_i64().map(|x| x as int)
|
||||
fn read_int(&mut self) -> DecodingResult<int> {
|
||||
Ok(try!(self.read_i64().map(|x| x as int)))
|
||||
}
|
||||
fn read_i64(&mut self) -> IoResult<i64> {
|
||||
self.reader.read_be_i64()
|
||||
fn read_i64(&mut self) -> DecodingResult<i64> {
|
||||
Ok(try!(self.reader.read_be_i64()))
|
||||
}
|
||||
fn read_i32(&mut self) -> IoResult<i32> {
|
||||
self.reader.read_be_i32()
|
||||
fn read_i32(&mut self) -> DecodingResult<i32> {
|
||||
Ok(try!(self.reader.read_be_i32()))
|
||||
}
|
||||
fn read_i16(&mut self) -> IoResult<i16> {
|
||||
self.reader.read_be_i16()
|
||||
fn read_i16(&mut self) -> DecodingResult<i16> {
|
||||
Ok(try!(self.reader.read_be_i16()))
|
||||
}
|
||||
fn read_i8(&mut self) -> IoResult<i8> {
|
||||
self.reader.read_i8()
|
||||
fn read_i8(&mut self) -> DecodingResult<i8> {
|
||||
Ok(try!(self.reader.read_i8()))
|
||||
}
|
||||
fn read_bool(&mut self) -> IoResult<bool> {
|
||||
match try!(self.reader.read_i8()) {
|
||||
fn read_bool(&mut self) -> DecodingResult<bool> {
|
||||
let x = try!(self.reader.read_u8());
|
||||
match x {
|
||||
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> {
|
||||
self.reader.read_be_f64()
|
||||
fn read_f64(&mut self) -> DecodingResult<f64> {
|
||||
Ok(try!(self.reader.read_be_f64()))
|
||||
}
|
||||
fn read_f32(&mut self) -> IoResult<f32> {
|
||||
self.reader.read_be_f32()
|
||||
fn read_f32(&mut self) -> DecodingResult<f32> {
|
||||
Ok(try!(self.reader.read_be_f32()))
|
||||
}
|
||||
fn read_char(&mut self) -> IoResult<char> {
|
||||
self.reader.read_char()
|
||||
fn read_char(&mut self) -> DecodingResult<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 vector = try!(self.reader.read_exact(len));
|
||||
String::from_utf8(vector).map_err(|e| IoError {
|
||||
kind: OtherIoError,
|
||||
desc: "invalid utf-8",
|
||||
detail: e.detail()
|
||||
})
|
||||
match String::from_utf8(vector) {
|
||||
Ok(s) => Ok(s),
|
||||
Err(err) => Err(DecodingError::InvalidBytes(InvalidBytes {
|
||||
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)
|
||||
}
|
||||
fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> IoResult<T> where
|
||||
F: FnMut(&mut DecoderReader<'a, R>, uint) -> IoResult<T> {
|
||||
fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodingResult<T> where
|
||||
F: FnMut(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
|
||||
let id = try!(self.read_u32());
|
||||
let id = id as uint;
|
||||
if id >= names.len() {
|
||||
Err(IoError {
|
||||
kind: OtherIoError,
|
||||
Err(DecodingError::InvalidBytes(InvalidBytes {
|
||||
desc: "out of bounds tag when reading enum variant",
|
||||
detail: Some(format!("Expected tag < {}, got {}", names.len(), id))
|
||||
})
|
||||
}))
|
||||
} else {
|
||||
f(self, id)
|
||||
}
|
||||
}
|
||||
fn read_enum_variant_arg<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_enum_variant_arg<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> IoResult<T> where
|
||||
F: FnMut(&mut DecoderReader<'a, R>, uint) -> IoResult<T> {
|
||||
fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodingResult<T> where
|
||||
F: FnMut(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
|
||||
self.read_enum_variant(names, f)
|
||||
}
|
||||
fn read_enum_struct_variant_field<T, F>(&mut self,
|
||||
_: &str,
|
||||
f_idx: uint,
|
||||
f: F)
|
||||
-> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
-> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
self.read_enum_variant_arg(f_idx, f)
|
||||
}
|
||||
fn read_struct<T, F>(&mut self, _: &str, _: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_struct<T, F>(&mut self, _: &str, _: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_struct_field<T, F>(&mut self,
|
||||
_: &str,
|
||||
_: uint,
|
||||
f: F)
|
||||
-> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
-> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_tuple<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_tuple<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_tuple_arg<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_tuple_arg<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_tuple_struct<T, F>(&mut self, _: &str, len: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_tuple_struct<T, F>(&mut self, _: &str, len: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
self.read_tuple(len, f)
|
||||
}
|
||||
fn read_tuple_struct_arg<T, F>(&mut self, a_idx: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_tuple_struct_arg<T, F>(&mut self, a_idx: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
self.read_tuple_arg(a_idx, f)
|
||||
}
|
||||
fn read_option<T, F>(&mut self, mut f: F) -> IoResult<T> where
|
||||
F: FnMut(&mut DecoderReader<'a, R>, bool) -> IoResult<T> {
|
||||
match try!(self.reader.read_u8()) {
|
||||
fn read_option<T, F>(&mut self, mut f: F) -> DecodingResult<T> where
|
||||
F: FnMut(&mut DecoderReader<'a, R>, bool) -> DecodingResult<T> {
|
||||
let x = try!(self.reader.read_u8());
|
||||
match x {
|
||||
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
|
||||
F: FnOnce(&mut DecoderReader<'a, R>, uint) -> IoResult<T> {
|
||||
fn read_seq<T, F>(&mut self, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
|
||||
let len = try!(self.read_uint());
|
||||
f(self, len)
|
||||
}
|
||||
fn read_seq_elt<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_seq_elt<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_map<T, F>(&mut self, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>, uint) -> IoResult<T> {
|
||||
fn read_map<T, F>(&mut self, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
|
||||
let len = try!(self.read_uint());
|
||||
f(self, len)
|
||||
}
|
||||
fn read_map_elt_key<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_map_elt_key<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_map_elt_val<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
fn read_map_elt_val<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn error(&mut self, err: &str) -> IoError {
|
||||
IoError {
|
||||
kind: OtherIoError,
|
||||
desc: "failure decoding or something, I don't know",
|
||||
detail: Some(err.to_string())
|
||||
}
|
||||
fn error(&mut self, err: &str) -> DecodingError {
|
||||
DecodingError::InvalidBytes(InvalidBytes {
|
||||
desc: "user-induced error",
|
||||
detail: Some(err.to_string()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
32
src/test.rs
32
src/test.rs
|
|
@ -13,6 +13,8 @@ use rustc_serialize::{
|
|||
use super::{
|
||||
encode,
|
||||
decode,
|
||||
DecodingError,
|
||||
DecodingResult
|
||||
};
|
||||
use super::SizeLimit::Infinite;
|
||||
|
||||
|
|
@ -49,7 +51,6 @@ fn test_numbers() {
|
|||
fn test_string() {
|
||||
the_same("".to_string());
|
||||
the_same("a".to_string());
|
||||
the_same("ƒoo".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -166,12 +167,25 @@ fn unicode() {
|
|||
the_same("aåååååååa".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_unicode() {
|
||||
// This is a malformed message that contains bad utf8.
|
||||
// The decoding should return Err but not panic.
|
||||
let encoded = vec![0,0,0,0, 0,0,0,2, 97, 195];
|
||||
let decoded: Result<String, _> = decode(encoded, Infinite);
|
||||
|
||||
assert!(decoded.is_err());
|
||||
#[cfg(test)]
|
||||
fn is_invalid_bytes<T>(res: DecodingResult<T>) {
|
||||
match res {
|
||||
Ok(_) => panic!("Expecting error"),
|
||||
Err(DecodingError::IoError(_)) => panic!("Expecting InvalidBytes"),
|
||||
Err(DecodingError::InvalidBytes(_)) => {},
|
||||
}
|
||||
}
|
||||
|
||||
#[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));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue