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
109
/*! Errors enum for DHT packets.
*/

use std::{str, fmt};

use failure::{Backtrace, Context, Fail};
use nom::{error::ErrorKind, Err};

use std::convert::From;
use std::io::Error as IoError;
use std::io::ErrorKind as IoErrorKind;

/// Error that can happen when calling `get_payload` of packet.
#[derive(Debug)]
pub struct GetPayloadError {
    ctx: Context<GetPayloadErrorKind>,
}

impl GetPayloadError {
    /// Return the kind of this error.
    pub fn kind(&self) -> &GetPayloadErrorKind {
        self.ctx.get_context()
    }

    pub(crate) fn decrypt() -> GetPayloadError {
        GetPayloadError::from(GetPayloadErrorKind::Decrypt)
    }

    pub(crate) fn deserialize(e: Err<(&[u8], ErrorKind)>, payload: Vec<u8>) -> GetPayloadError {
        GetPayloadError::from(GetPayloadErrorKind::Deserialize { error: e.to_owned(), payload })
    }
}

impl Fail for GetPayloadError {
    fn cause(&self) -> Option<&dyn Fail> {
        self.ctx.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.ctx.backtrace()
    }
}

impl fmt::Display for GetPayloadError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.ctx.fmt(f)
    }
}

/// The specific kind of error that can occur.
#[derive(Clone, Debug, Eq, PartialEq, Fail)]
pub enum GetPayloadErrorKind {
    /// Error indicates that received payload of encrypted packet can't be decrypted
    #[fail(display = "Decrypt payload error")]
    Decrypt,
    /// Error indicates that decrypted payload of packet can't be parsed
    #[fail(display = "Deserialize payload error: {:?}, data: {:?}", error, payload)]
    Deserialize {
        /// Parsing error
        error: nom::Err<(Vec<u8>, ErrorKind)>,
        /// Received payload of packet
        payload: Vec<u8>,
    }
}

impl From<GetPayloadErrorKind> for GetPayloadError {
    fn from(kind: GetPayloadErrorKind) -> GetPayloadError {
        GetPayloadError::from(Context::new(kind))
    }
}

impl From<Context<GetPayloadErrorKind>> for GetPayloadError {
    fn from(ctx: Context<GetPayloadErrorKind>) -> GetPayloadError {
        GetPayloadError { ctx }
    }
}

/// From trait for temporary use during transition from io:Error to custom enum error of failure crate
impl From<GetPayloadError> for IoError {
    fn from(item: GetPayloadError) -> Self {
        IoError::new(IoErrorKind::Other, format!("GetPayloadError occured. error: {:?}", item))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use nom::Needed;

    #[test]
    fn get_payload_error() {
        let error = GetPayloadError::deserialize(Err::Error((&[], ErrorKind::Eof)), vec![1, 2, 3, 4]);
        assert!(error.cause().is_none());
        assert!(error.backtrace().is_some());
        assert_eq!(format!("{}", error), "Deserialize payload error: Error(([], Eof)), data: [1, 2, 3, 4]".to_owned());
    }

    #[test]
    fn get_payload_error_kind() {
        let decrypt = GetPayloadErrorKind::Decrypt;
        assert_eq!(format!("{}", decrypt), "Decrypt payload error".to_owned());

        let incomplete = GetPayloadErrorKind::Deserialize { error: Err::Incomplete(Needed::Size(5)), payload: vec![1, 2, 3, 4] };
        assert_eq!(format!("{}", incomplete), "Deserialize payload error: Incomplete(Size(5)), data: [1, 2, 3, 4]".to_owned());

        let deserialize = GetPayloadErrorKind::Deserialize { error: Err::Error((vec![], ErrorKind::Eof)), payload: vec![1, 2, 3, 4] };
        assert_eq!(format!("{}", deserialize), "Deserialize payload error: Error(([], Eof)), data: [1, 2, 3, 4]".to_owned());
    }
}