diff --git a/lib/src/config/error.rs b/lib/src/config/error.rs index f125724f..8a814de2 100644 --- a/lib/src/config/error.rs +++ b/lib/src/config/error.rs @@ -1,6 +1,9 @@ use std::path::PathBuf; +use std::error::Error; +use std::fmt; use super::Environment; +use self::ConfigError::*; use term_painter::Color::White; use term_painter::ToStyle; @@ -56,8 +59,6 @@ pub enum ConfigError { impl ConfigError { /// Prints this configuration error with Rocket formatting. pub fn pretty_print(&self) { - use self::ConfigError::*; - let valid_envs = Environment::valid(); match *self { BadCWD => error!("couldn't get current working directory"), @@ -106,10 +107,47 @@ impl ConfigError { /// Whether this error is of `NotFound` variant. #[inline(always)] pub fn is_not_found(&self) -> bool { - use self::ConfigError::*; match *self { NotFound => true, _ => false } } } + +impl fmt::Display for ConfigError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + BadCWD => write!(f, "couldn't get current working directory"), + NotFound => write!(f, "config file was not found"), + IOError => write!(f, "I/O error while reading the config file"), + BadFilePath(ref p, _) => write!(f, "{:?} is not a valid config path", p), + BadEnv(ref e) => write!(f, "{:?} is not a valid `ROCKET_ENV` value", e), + ParseError(..) => write!(f, "the config file contains invalid TOML"), + BadEntry(ref e, _) => { + write!(f, "{:?} is not a valid `[environment]` entry", e) + } + BadType(ref n, e, a, _) => { + write!(f, "type mismatch for '{}'. expected {}, found {}", n, e, a) + } + BadEnvVal(ref k, ref v, _) => { + write!(f, "environment variable '{}={}' could not be parsed", k, v) + } + } + } +} + +impl Error for ConfigError { + fn description(&self) -> &str { + match *self { + BadCWD => "the current working directory could not be determined", + NotFound => "config file was not found", + IOError => "there was an I/O error while reading the config file", + BadFilePath(..) => "the config file path is invalid", + BadEntry(..) => "an environment specified as `[environment]` is invalid", + BadEnv(..) => "the environment specified in `ROCKET_ENV` is invalid", + ParseError(..) => "the config file contains invalid TOML", + BadType(..) => "a key was specified with a value of the wrong type", + BadEnvVal(..) => "an environment variable could not be parsed", + } + } +}