ice_rs/
errors.rs

1use std::fmt::Display;
2use crate::encoding::FromBytes;
3
4/// A `ProtocolError` indicates a problem related to the
5/// ice protocol. It may be unexpected messages or problems
6/// while encoding or decoding objects.
7#[derive(Debug)]
8pub struct ProtocolError {
9    detail: String
10}
11
12impl ProtocolError {
13    pub fn new(detail: &str) -> ProtocolError {
14        ProtocolError {
15            detail: String::from(detail)
16        }
17    }
18}
19
20/// A `ParsingError` appears when a problem occurs parsing ice
21/// files.
22#[derive(Debug)]
23pub struct ParsingError {
24    detail: String
25}
26
27impl ParsingError {
28    pub fn new(detail: &str) -> ParsingError {
29        ParsingError {
30            detail: String::from(detail)
31        }
32    }
33}
34
35/// A `PropertyError` appears when a requested property is not
36/// existing.
37#[derive(Debug)]
38pub struct PropertyError {
39    missing_key: String
40}
41
42impl PropertyError {
43    pub fn new(missing_key: &str) -> PropertyError {
44        PropertyError {
45            missing_key: String::from(missing_key)
46        }
47    }
48}
49
50/// A `RemoteException` is raised when the remote application
51/// raises any error that is not an `UserError`.
52#[derive(Debug)]
53pub struct RemoteException {
54    pub cause: String
55}
56
57/// A `UserError` is an error that is defined in ice files.
58/// The generic type will be the defined error struct.
59#[derive(Debug)]
60pub struct UserError<T: std::fmt::Display> {
61    pub exception: T
62}
63
64impl std::fmt::Display for ProtocolError {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(f, "ProtocolError: {}", self.detail)
67    }
68}
69
70impl std::fmt::Display for ParsingError {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        write!(f, "ParsingError!")
73    }
74}
75
76impl std::fmt::Display for PropertyError {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        write!(f, "PropertyError!")
79    }
80}
81
82impl std::fmt::Display for RemoteException {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        write!(f, "RemoteException: {}", self.cause)
85    }
86}
87
88impl<T: Display> std::fmt::Display for UserError<T> {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        write!(f, "{}", self.exception)
91    }
92}
93
94
95impl std::error::Error for ProtocolError {}
96impl std::error::Error for ParsingError {}
97impl std::error::Error for RemoteException {}
98impl std::error::Error for PropertyError {}
99impl<T: std::fmt::Debug + Display + FromBytes> std::error::Error for UserError<T> {}
100
101// dummy needed, but should not get called
102impl FromBytes for ProtocolError {
103    fn from_bytes(bytes: &[u8], _read_bytes: &mut i32) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> where Self: Sized {
104        Ok(Self {
105            detail: String::from_utf8(bytes.to_vec())?
106        })
107    }
108}