Implement Display and Error for ConfigError.

Closes #189.
This commit is contained in:
Sergio Benitez 2017-02-15 01:32:53 -08:00
parent d8b90ebf5f
commit d8afb4c7fa
1 changed files with 41 additions and 3 deletions

View File

@ -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",
}
}
}