1use std::fmt::Display;
2use crate::encoding::FromBytes;
3
4#[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#[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#[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#[derive(Debug)]
53pub struct RemoteException {
54 pub cause: String
55}
56
57#[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
101impl 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}