Skip to content

Commit

Permalink
error: implement an Error instance for ConfigError
Browse files Browse the repository at this point in the history
  • Loading branch information
mathstuf committed Aug 22, 2015
1 parent a87b972 commit 5b65430
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Errors that can occur while parsing a configuration

use std::error::Error;
use std::fmt;
use std::io::Error as IoError;
use parser::ParseError;
Expand All @@ -12,7 +13,9 @@ pub struct ConfigError {
/// A descriptive message about the error
pub desc: &'static str,
/// Error details, if available
pub detail: Option<String>
pub detail: Option<String>,

err_desc: String
}

/// Possible error kinds
Expand All @@ -26,10 +29,13 @@ pub enum ConfigErrorKind {
}

fn mk_error<E: fmt::Display>(kind: ConfigErrorKind, desc: &'static str, err: E) -> ConfigError {
let err_desc = format!("{:?} {}: {}", kind, desc, err);
ConfigError {
kind: kind,
desc: desc,
detail: Some(format!("{}", err))
detail: Some(format!("{}", err)),

err_desc: err_desc
}
}

Expand All @@ -42,3 +48,16 @@ pub fn from_io_err(err: IoError) -> ConfigError {
pub fn from_parse_err(err: ParseError) -> ConfigError {
mk_error(ConfigErrorKind::ParseError, "Syntax error", err)
}

impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}", self.description()));
Ok(())
}
}

impl Error for ConfigError {
fn description(&self) -> &str {
&self.err_desc[..]
}
}

0 comments on commit 5b65430

Please sign in to comment.