Skip to content

Commit 7c152f8

Browse files
committed
Add Error impls to a few key error types
1 parent 6815c2e commit 7c152f8

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
@@ -222,7 +222,9 @@ responding to errors that may occur while attempting to read the numbers.
222222
#![deny(unused_must_use)]
223223

224224
use char::Char;
225+
use clone::Clone;
225226
use default::Default;
227+
use error::{FromError, Error};
226228
use fmt;
227229
use int;
228230
use iter::Iterator;
@@ -433,6 +435,22 @@ impl fmt::Show for IoError {
433435
}
434436
}
435437

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

src/libstd/os.rs

+14
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
#![allow(non_snake_case)]
3333

3434
use clone::Clone;
35+
use error::{FromError, Error};
3536
use fmt;
3637
use io::{IoResult, IoError};
3738
use iter::Iterator;
3839
use libc::{c_void, c_int};
3940
use libc;
41+
use boxed::Box;
4042
use ops::Drop;
4143
use option::{Some, None, Option};
4244
use os;
@@ -48,6 +50,7 @@ use slice::{AsSlice, ImmutableSlice, MutableSlice, ImmutablePartialEqSlice};
4850
use slice::CloneableVector;
4951
use str::{Str, StrSlice, StrAllocating};
5052
use string::String;
53+
use to_string::ToString;
5154
use sync::atomic::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
5255
use vec::Vec;
5356

@@ -1437,6 +1440,17 @@ impl fmt::Show for MapError {
14371440
}
14381441
}
14391442

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

0 commit comments

Comments
 (0)