Skip to content

Commit bc1bc9d

Browse files
committed
Add Error impls to a few key error types
1 parent c5a9e81 commit bc1bc9d

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

src/libserialize/base64.rs

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! Base64 binary-to-text encoding
1414
use std::fmt;
1515
use std::string;
16+
use std::error;
1617

1718
/// Available encoding character sets
1819
pub enum CharacterSet {
@@ -178,6 +179,19 @@ impl fmt::Show for FromBase64Error {
178179
}
179180
}
180181

182+
impl error::Error for FromBase64Error {
183+
fn description(&self) -> &str {
184+
match *self {
185+
InvalidBase64Byte(_, _) => "invalid character",
186+
InvalidBase64Length => "invalid length",
187+
}
188+
}
189+
190+
fn detail(&self) -> Option<String> {
191+
Some(self.to_string())
192+
}
193+
}
194+
181195
impl<'a> FromBase64 for &'a str {
182196
/**
183197
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)

src/libserialize/hex.rs

+15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! Hex binary-to-text encoding
1414
use std::fmt;
1515
use std::string;
16+
use std::error;
1617

1718
/// A trait for converting a value to hexadecimal encoding
1819
pub trait ToHex {
@@ -77,6 +78,20 @@ impl fmt::Show for FromHexError {
7778
}
7879
}
7980

81+
impl error::Error for FromHexError {
82+
fn description(&self) -> &str {
83+
match *self {
84+
InvalidHexCharacter(_, _) => "invalid character",
85+
InvalidHexLength => "invalid length",
86+
}
87+
}
88+
89+
fn detail(&self) -> Option<String> {
90+
Some(self.to_string())
91+
}
92+
}
93+
94+
8095
impl<'a> FromHex for &'a str {
8196
/**
8297
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)

src/libserialize/json.rs

+5
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ fn io_error_to_error(io: io::IoError) -> ParserError {
313313
IoError(io.kind, io.desc)
314314
}
315315

316+
impl std::error::Error for DecoderError {
317+
fn description(&self) -> &str { "decoder error" }
318+
fn detail(&self) -> Option<std::string::String> { Some(self.to_string()) }
319+
}
320+
316321
pub type EncodeResult = io::IoResult<()>;
317322
pub type DecodeResult<T> = Result<T, DecoderError>;
318323

src/libstd/io/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ responding to errors that may occur while attempting to read the numbers.
223223

224224
use char::Char;
225225
use collections::Collection;
226+
use clone::Clone;
226227
use default::Default;
228+
use error::{FromError, Error};
227229
use fmt;
228230
use int;
229231
use iter::Iterator;
@@ -434,6 +436,22 @@ impl fmt::Show for IoError {
434436
}
435437
}
436438

439+
impl Error for IoError {
440+
fn description(&self) -> &str {
441+
self.desc
442+
}
443+
444+
fn detail(&self) -> Option<String> {
445+
self.detail.clone()
446+
}
447+
}
448+
449+
impl FromError<IoError> for Box<Error> {
450+
fn from_error(err: IoError) -> Box<Error> {
451+
box err
452+
}
453+
}
454+
437455
/// A list specifying general categories of I/O error.
438456
#[deriving(PartialEq, Eq, Clone, Show)]
439457
pub enum IoErrorKind {

src/libstd/os.rs

+14
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333

3434
use clone::Clone;
3535
use collections::{Collection, MutableSeq};
36+
use error::{FromError, Error};
3637
use fmt;
3738
use io::{IoResult, IoError};
3839
use iter::Iterator;
3940
use libc::{c_void, c_int};
4041
use libc;
42+
use boxed::Box;
4143
use ops::Drop;
4244
use option::{Some, None, Option};
4345
use os;
@@ -49,6 +51,7 @@ use slice::{AsSlice, ImmutableSlice, MutableSlice, ImmutablePartialEqSlice};
4951
use slice::CloneableVector;
5052
use str::{Str, StrSlice, StrAllocating};
5153
use string::String;
54+
use to_string::ToString;
5255
use sync::atomic::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
5356
use vec::Vec;
5457

@@ -1438,6 +1441,17 @@ impl fmt::Show for MapError {
14381441
}
14391442
}
14401443

1444+
impl Error for MapError {
1445+
fn description(&self) -> &str { "memory map error" }
1446+
fn detail(&self) -> Option<String> { Some(self.to_string()) }
1447+
}
1448+
1449+
impl FromError<MapError> for Box<Error> {
1450+
fn from_error(err: MapError) -> Box<Error> {
1451+
box err
1452+
}
1453+
}
1454+
14411455
#[cfg(unix)]
14421456
impl MemoryMap {
14431457
/// Create a new mapping with the given `options`, at least `min_len` bytes

0 commit comments

Comments
 (0)