1 | //! Error types used and generated by Calloop. |
2 | //! |
3 | //! This module contains error types for Calloop's operations. They are designed |
4 | //! to make it easy to deal with errors arising from Calloop's internal I/O and |
5 | //! other operations. |
6 | //! |
7 | //! There are two top-level error types: |
8 | //! |
9 | //! - [`Error`]: used by callback functions, internal operations, and some event |
10 | //! loop API calls |
11 | //! |
12 | //! - [`InsertError`]: used primarily by the [`insert_source()`] method when an |
13 | //! event source cannot be added to the loop and needs to be given back to the |
14 | //! caller |
15 | //! |
16 | //! [`insert_source()`]: crate::LoopHandle::insert_source() |
17 | |
18 | use std::fmt::{self, Debug, Formatter}; |
19 | |
20 | /// The primary error type used by Calloop covering internal errors and I/O |
21 | /// errors that arise during loop operations such as source registration or |
22 | /// event dispatching. |
23 | #[derive (thiserror::Error, Debug)] |
24 | pub enum Error { |
25 | /// When an event source is registered (or re- or un-registered) with the |
26 | /// event loop, this error variant will occur if the token Calloop uses to |
27 | /// keep track of the event source is not valid. |
28 | #[error("invalid token provided to internal function" )] |
29 | InvalidToken, |
30 | |
31 | /// This variant wraps a [`std::io::Error`], which might arise from |
32 | /// Calloop's internal operations. |
33 | #[error("underlying IO error" )] |
34 | IoError(#[from] std::io::Error), |
35 | |
36 | /// Any other unexpected error kind (most likely from a user implementation of |
37 | /// [`EventSource::process_events()`]) will be wrapped in this. |
38 | /// |
39 | /// [`EventSource::process_events()`]: crate::EventSource::process_events() |
40 | #[error("other error during loop operation" )] |
41 | OtherError(#[from] Box<dyn std::error::Error + Sync + Send>), |
42 | } |
43 | |
44 | impl From<Error> for std::io::Error { |
45 | /// Converts Calloop's error type into a [`std::io::Error`]. |
46 | fn from(err: Error) -> Self { |
47 | match err { |
48 | Error::IoError(source: Error) => source, |
49 | Error::InvalidToken => Self::new(kind:std::io::ErrorKind::InvalidInput, error:err.to_string()), |
50 | Error::OtherError(source: Box) => Self::new(kind:std::io::ErrorKind::Other, error:source), |
51 | } |
52 | } |
53 | } |
54 | |
55 | /// [`Result`] alias using Calloop's error type. |
56 | pub type Result<T> = core::result::Result<T, Error>; |
57 | |
58 | /// An error generated when trying to insert an event source |
59 | #[derive (thiserror::Error)] |
60 | #[error("error inserting event source" )] |
61 | pub struct InsertError<T> { |
62 | /// The source that could not be inserted |
63 | pub inserted: T, |
64 | /// The generated error |
65 | #[source] |
66 | pub error: Error, |
67 | } |
68 | |
69 | impl<T> Debug for InsertError<T> { |
70 | #[cfg_attr (feature = "nightly_coverage" , coverage(off))] |
71 | fn fmt(&self, formatter: &mut Formatter) -> core::result::Result<(), fmt::Error> { |
72 | write!(formatter, " {:?}" , self.error) |
73 | } |
74 | } |
75 | |
76 | impl<T> From<InsertError<T>> for crate::Error { |
77 | /// Converts the [`InsertError`] into Calloop's error type, throwing away |
78 | /// the contained source. |
79 | #[cfg_attr (feature = "nightly_coverage" , coverage(off))] |
80 | fn from(e: InsertError<T>) -> crate::Error { |
81 | e.error |
82 | } |
83 | } |
84 | |