1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
use std::error;
use std::fmt;
use crate::ErrorKind;

#[derive(Debug)]
pub struct Error {
    kind: ErrorKind,
    message: String,
    status: i32,
    source: Option<Box<dyn error::Error + 'static>>,
}

impl Error {

    pub fn new(kind: ErrorKind) -> Self {
        Self {
            message: error_message(&kind),
            status: error_status(&kind),
            kind,
            source: None,
        }
    }

    pub fn with_source<E>(source: E, kind: ErrorKind) -> Self
        where
        E: std::error::Error + 'static,
    {
        Self {
            message: error_message(&kind),
            status: error_status(&kind),
            kind,
            source: Some(Box::new(source)),
        }
    }

    pub fn kind(&self) -> &ErrorKind {
        &self.kind
    }

    pub fn message(&self) -> &str {
        &self.message
    }

    pub fn status(&self) -> &i32 {
        &self.status
    }

    pub fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        self.source.as_ref().map(|e| e.as_ref())
    }
}

impl std::default::Default for Error {
    fn default() -> Self {
        Self {
            message: error_message(&ErrorKind::GeneralError),
            status: error_status(&ErrorKind::GeneralError),
            kind: ErrorKind::GeneralError,
            source: None,
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        self.source.as_ref().map(|e| e.as_ref())
    }
}

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        self.kind == other.kind
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.message())
    }
}

fn error_message(kind: &ErrorKind) -> String {
    match kind {
        ErrorKind::GeneralError => format!("Unknown error occurred while processing."),
        ErrorKind::UnknownCommand(name) => format!("The requested command `{}` does not exist.", name),
        ErrorKind::MissingCommandResolver(name) => format!("The requested command `{}` does not have a resolver.", name),
        ErrorKind::CommandFailed(name) => format!("The requested command `{}` failed to execute.", name),
        ErrorKind::UnknownFlag(name) => format!("The provided flag `{}` does not exist.", name),
        ErrorKind::MissingFlagValue(name) => format!("The provided flag `{}` should have a value.", name),
        ErrorKind::InvalidFlagValue(name) => format!("The provided flag `{}` has invalid value.", name),
        ErrorKind::InvalidParamValue(index) => format!("The provided param `{}` has invalid value.", index),
        ErrorKind::ToManyParams(expected, found) => format!("Too many parameters where provided (received {} but accepts only {}).", expected, found),
    }
}

fn error_status(kind: &ErrorKind) -> i32 {
    match kind { // [64 - 113]
        ErrorKind::GeneralError => 1,
        ErrorKind::UnknownCommand(_) => 65,
        ErrorKind::MissingCommandResolver(_) => 66,
        ErrorKind::CommandFailed(_) => 67,
        ErrorKind::UnknownFlag(_) => 68,
        ErrorKind::MissingFlagValue(_) => 69,
        ErrorKind::InvalidFlagValue(_) => 70,
        ErrorKind::InvalidParamValue(_) => 71,
        ErrorKind::ToManyParams(_, _) => 72,
    }
}