ferroui_remote_protocol/
error.rs1use std::fmt;
6use std::io;
7use std::sync::Arc;
8
9use crate::metsys_bson::BsonException;
10
11#[derive(Clone, Debug)]
14pub enum Error {
15 Bson(BsonException),
17 EndOfStream,
20 InvalidOperation(String),
22 InvalidCast(String),
25 KeyNotFound(String),
28 Argument(String),
30 Overflow(String),
32 Format(String),
34 NullReference,
36 Io(Arc<io::Error>),
39 Handler(String),
42}
43
44impl fmt::Display for Error {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match self {
47 Error::Bson(e) => e.fmt(f),
48 Error::EndOfStream => f.write_str("Unable to read beyond the end of the stream."),
49 Error::InvalidOperation(message)
50 | Error::InvalidCast(message)
51 | Error::KeyNotFound(message)
52 | Error::Argument(message)
53 | Error::Overflow(message)
54 | Error::Format(message)
55 | Error::Handler(message) => f.write_str(message),
56 Error::NullReference => f.write_str("Object reference not set to an instance of an object."),
57 Error::Io(e) => e.fmt(f),
58 }
59 }
60}
61
62impl std::error::Error for Error {
63 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
64 match self {
65 Error::Bson(e) => Some(e),
66 Error::Io(e) => Some(&**e),
67 _ => None,
68 }
69 }
70}
71
72impl From<BsonException> for Error {
73 fn from(e: BsonException) -> Self {
74 Error::Bson(e)
75 }
76}
77
78impl From<io::Error> for Error {
79 fn from(e: io::Error) -> Self {
80 if e.kind() == io::ErrorKind::UnexpectedEof {
81 Error::EndOfStream
82 } else {
83 Error::Io(Arc::new(e))
84 }
85 }
86}
87
88pub(crate) fn catch_handler(f: impl FnOnce()) -> Result<(), Error> {
91 std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).map_err(|payload| {
92 let message = if let Some(text) = payload.downcast_ref::<&str>() {
93 (*text).to_string()
94 } else if let Some(text) = payload.downcast_ref::<String>() {
95 text.clone()
96 } else {
97 "A handler panicked.".to_string()
98 };
99 Error::Handler(message)
100 })
101}