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::fmt::Display;
use crate::encoding::FromBytes;

/// A `ProtocolError` indicates a problem related to the
/// ice protocol. It may be unexpected messages or problems
/// while encoding or decoding objects.
#[derive(Debug)]
pub struct ProtocolError {
    detail: String
}

impl ProtocolError {
    pub fn new(detail: &str) -> ProtocolError {
        ProtocolError {
            detail: String::from(detail)
        }
    }
}

/// A `ParsingError` appears when a problem occurs parsing ice
/// files.
#[derive(Debug)]
pub struct ParsingError {
    detail: String
}

impl ParsingError {
    pub fn new(detail: &str) -> ParsingError {
        ParsingError {
            detail: String::from(detail)
        }
    }
}

/// A `PropertyError` appears when a requested property is not
/// existing.
#[derive(Debug)]
pub struct PropertyError {
    missing_key: String
}

impl PropertyError {
    pub fn new(missing_key: &str) -> PropertyError {
        PropertyError {
            missing_key: String::from(missing_key)
        }
    }
}

/// A `RemoteException` is raised when the remote application
/// raises any error that is not an `UserError`.
#[derive(Debug)]
pub struct RemoteException {
    pub cause: String
}

/// A `UserError` is an error that is defined in ice files.
/// The generic type will be the defined error struct.
#[derive(Debug)]
pub struct UserError<T: std::fmt::Display> {
    pub exception: T
}

impl std::fmt::Display for ProtocolError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ProtocolError: {}", self.detail)
    }
}

impl std::fmt::Display for ParsingError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ParsingError!")
    }
}

impl std::fmt::Display for PropertyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "PropertyError!")
    }
}

impl std::fmt::Display for RemoteException {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "RemoteException: {}", self.cause)
    }
}

impl<T: Display> std::fmt::Display for UserError<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.exception)
    }
}


impl std::error::Error for ProtocolError {}
impl std::error::Error for ParsingError {}
impl std::error::Error for RemoteException {}
impl std::error::Error for PropertyError {}
impl<T: std::fmt::Debug + Display + FromBytes> std::error::Error for UserError<T> {}

// dummy needed, but should not get called
impl FromBytes for ProtocolError {
    fn from_bytes(bytes: &[u8], _read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> where Self: Sized {
        Ok(Self {
            detail: String::from_utf8(bytes.to_vec())?
        })
    }
}