Add Error impls to a few key error types

This commit is contained in:
Aaron Turon 2014-10-03 14:24:49 -07:00
parent 6815c2e8e8
commit 7c152f870d
5 changed files with 66 additions and 0 deletions

View File

@ -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<String> {
Some(self.to_string())
}
}
impl<'a> FromBase64 for &'a str {
/**
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)

View File

@ -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<String> {
Some(self.to_string())
}
}
impl<'a> FromHex for &'a str {
/**
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)

View File

@ -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<std::string::String> { Some(self.to_string()) }
}
pub type EncodeResult = io::IoResult<()>;
pub type DecodeResult<T> = Result<T, DecoderError>;

View File

@ -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<String> {
self.detail.clone()
}
}
impl FromError<IoError> for Box<Error> {
fn from_error(err: IoError) -> Box<Error> {
box err
}
}
/// A list specifying general categories of I/O error.
#[deriving(PartialEq, Eq, Clone, Show)]
pub enum IoErrorKind {

View File

@ -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<String> { Some(self.to_string()) }
}
impl FromError<MapError> for Box<Error> {
fn from_error(err: MapError) -> Box<Error> {
box err
}
}
#[cfg(unix)]
impl MemoryMap {
/// Create a new mapping with the given `options`, at least `min_len` bytes