diff --git a/src/libserialize/base64.rs b/src/libserialize/base64.rs index c999157b89a..e69a0ea7929 100644 --- a/src/libserialize/base64.rs +++ b/src/libserialize/base64.rs @@ -13,6 +13,7 @@ //! Base64 binary-to-text encoding use std::fmt; use std::string; +use std::error; /// Available encoding character sets pub enum CharacterSet { @@ -178,6 +179,19 @@ impl fmt::Show for FromBase64Error { } } +impl error::Error for FromBase64Error { + fn description(&self) -> &str { + match *self { + InvalidBase64Byte(_, _) => "invalid character", + InvalidBase64Length => "invalid length", + } + } + + fn detail(&self) -> Option { + Some(self.to_string()) + } +} + impl<'a> FromBase64 for &'a str { /** * Convert any base64 encoded string (literal, `@`, `&`, or `~`) diff --git a/src/libserialize/hex.rs b/src/libserialize/hex.rs index ffe63f738cf..b591d35c67c 100644 --- a/src/libserialize/hex.rs +++ b/src/libserialize/hex.rs @@ -13,6 +13,7 @@ //! Hex binary-to-text encoding use std::fmt; use std::string; +use std::error; /// A trait for converting a value to hexadecimal encoding pub trait ToHex { @@ -77,6 +78,20 @@ impl fmt::Show for FromHexError { } } +impl error::Error for FromHexError { + fn description(&self) -> &str { + match *self { + InvalidHexCharacter(_, _) => "invalid character", + InvalidHexLength => "invalid length", + } + } + + fn detail(&self) -> Option { + Some(self.to_string()) + } +} + + impl<'a> FromHex for &'a str { /** * Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`) diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index 06f934c075d..cde2dfac378 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -313,6 +313,11 @@ fn io_error_to_error(io: io::IoError) -> ParserError { IoError(io.kind, io.desc) } +impl std::error::Error for DecoderError { + fn description(&self) -> &str { "decoder error" } + fn detail(&self) -> Option { Some(self.to_string()) } +} + pub type EncodeResult = io::IoResult<()>; pub type DecodeResult = Result; diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index c6f237ff1da..3dcd8d792a4 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -222,7 +222,9 @@ responding to errors that may occur while attempting to read the numbers. #![deny(unused_must_use)] use char::Char; +use clone::Clone; use default::Default; +use error::{FromError, Error}; use fmt; use int; use iter::Iterator; @@ -433,6 +435,22 @@ impl fmt::Show for IoError { } } +impl Error for IoError { + fn description(&self) -> &str { + self.desc + } + + fn detail(&self) -> Option { + self.detail.clone() + } +} + +impl FromError for Box { + fn from_error(err: IoError) -> Box { + box err + } +} + /// A list specifying general categories of I/O error. #[deriving(PartialEq, Eq, Clone, Show)] pub enum IoErrorKind { diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 5b3c872d2b7..9846f7b653e 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -32,11 +32,13 @@ #![allow(non_snake_case)] use clone::Clone; +use error::{FromError, Error}; use fmt; use io::{IoResult, IoError}; use iter::Iterator; use libc::{c_void, c_int}; use libc; +use boxed::Box; use ops::Drop; use option::{Some, None, Option}; use os; @@ -48,6 +50,7 @@ use slice::{AsSlice, ImmutableSlice, MutableSlice, ImmutablePartialEqSlice}; use slice::CloneableVector; use str::{Str, StrSlice, StrAllocating}; use string::String; +use to_string::ToString; use sync::atomic::{AtomicInt, INIT_ATOMIC_INT, SeqCst}; use vec::Vec; @@ -1437,6 +1440,17 @@ impl fmt::Show for MapError { } } +impl Error for MapError { + fn description(&self) -> &str { "memory map error" } + fn detail(&self) -> Option { Some(self.to_string()) } +} + +impl FromError for Box { + fn from_error(err: MapError) -> Box { + box err + } +} + #[cfg(unix)] impl MemoryMap { /// Create a new mapping with the given `options`, at least `min_len` bytes