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
use std::{error, fmt, str};
use rustc_serialize::hex::FromHexError;
use rustc_serialize::json::EncoderError;
use curl;
use std::convert::From;

pub use self::ErrorKind::*;

/// Result alias to save typing
pub type SlackResult<T> = Result<T, SlackError>;

/// Different kinds of errors handled
#[derive(Clone, Debug)]
pub enum ErrorKind {
    /// slack response failed
    ErrSlackResp,
    /// slack response should be in utf8
    ErrUtf8(str::Utf8Error),
    /// couldn't convert value to Hex
    ErrFromHex(FromHexError),
    /// failed other hex color validations for input
    ErrHexColor,
    /// failed to encode payload
    ErrEncoder(EncoderError),
    /// curl error
    ErrCurl(curl::Error),
}

/// main Slack library error
#[derive(Debug)]
pub struct SlackError {
    /// kind of error
    pub kind: ErrorKind,
    /// description of error
    pub desc: String,
}

impl From<str::Utf8Error> for SlackError {
    fn from(err: str::Utf8Error) -> SlackError {
        SlackError {
            kind: ErrUtf8(err),
            desc: format!("{:?}", err),
        }
    }
}

impl From<EncoderError> for SlackError {
    fn from(err: EncoderError) -> SlackError {
        SlackError {
            kind: ErrEncoder(err),
            desc: format!("{:?}", err),
        }
    }
}

impl From<curl::Error> for SlackError {
    fn from(err: curl::Error) -> SlackError {
        SlackError {
            kind: ErrCurl(err.clone()),
            desc: format!("{:?}", err),
        }
    }
}

impl From<FromHexError> for SlackError {
    fn from(err: FromHexError) -> SlackError {
        SlackError {
            kind: ErrFromHex(err),
            desc: format!("{:?}", err),
        }
    }
}

impl<'a> From<(ErrorKind, &'a str)> for SlackError {
    fn from((kind, desc): (ErrorKind, &'a str)) -> SlackError {
        SlackError {
            kind: kind,
            desc: desc.to_owned(),
        }
    }
}

impl fmt::Display for SlackError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            _ => write!(f, "Invalid character '{}' at position {}", "ch", "idx"),
        }
    }
}

impl error::Error for SlackError {
    fn description(&self) -> &str {
        match self.kind {
            ErrUtf8(ref err) => err.description(),
            ErrFromHex(ref err) => err.description(),
            _ => &self.desc[..],
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match self.kind {
            ErrUtf8(ref err) => err.cause(),
            ErrFromHex(ref err) => err.cause(),
            _ => None,
        }
    }
}