winit_core/
error.rs

1use std::error::Error;
2use std::fmt::{self, Display};
3
4/// A general error that may occur while running or creating
5/// the event loop.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum EventLoopError {
9    /// The event loop can't be re-created.
10    RecreationAttempt,
11    /// Application has exit with an error status.
12    ExitFailure(i32),
13    /// Got unspecified OS-specific error during the request.
14    Os(OsError),
15    /// Creating the event loop with the requested configuration is not supported.
16    NotSupported(NotSupportedError),
17}
18
19impl fmt::Display for EventLoopError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Self::RecreationAttempt => {
23                write!(
24                    f,
25                    "EventLoop can't be recreated, only a single instance of it is supported (for \
26                     cross-platform compatibility)"
27                )
28            },
29            Self::Os(err) => err.fmt(f),
30            Self::ExitFailure(status) => write!(f, "Exit Failure: {status}"),
31            Self::NotSupported(err) => err.fmt(f),
32        }
33    }
34}
35
36impl Error for EventLoopError {
37    fn source(&self) -> Option<&(dyn Error + 'static)> {
38        if let Self::Os(err) = self { err.source() } else { None }
39    }
40}
41
42impl From<OsError> for EventLoopError {
43    fn from(value: OsError) -> Self {
44        Self::Os(value)
45    }
46}
47
48impl From<NotSupportedError> for EventLoopError {
49    fn from(value: NotSupportedError) -> Self {
50        Self::NotSupported(value)
51    }
52}
53
54/// A general error that may occur during a request to the windowing system.
55#[derive(Debug)]
56#[non_exhaustive]
57pub enum RequestError {
58    /// The request is not supported.
59    NotSupported(NotSupportedError),
60    /// The request was ignored by the operating system.
61    Ignored,
62    /// Got unspecified OS specific error during the request.
63    Os(OsError),
64}
65
66impl Display for RequestError {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        match self {
69            Self::NotSupported(err) => err.fmt(f),
70            Self::Ignored => write!(f, "The request was ignored"),
71            Self::Os(err) => err.fmt(f),
72        }
73    }
74}
75impl Error for RequestError {
76    fn source(&self) -> Option<&(dyn Error + 'static)> {
77        if let Self::Os(err) = self { err.source() } else { None }
78    }
79}
80
81impl From<NotSupportedError> for RequestError {
82    fn from(value: NotSupportedError) -> Self {
83        Self::NotSupported(value)
84    }
85}
86
87impl From<OsError> for RequestError {
88    fn from(value: OsError) -> Self {
89        Self::Os(value)
90    }
91}
92
93/// The requested operation is not supported.
94#[derive(Debug)]
95pub struct NotSupportedError {
96    /// The reason why a certain operation is not supported.
97    reason: &'static str,
98}
99
100impl NotSupportedError {
101    pub fn new(reason: &'static str) -> Self {
102        Self { reason }
103    }
104}
105
106impl fmt::Display for NotSupportedError {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        write!(f, "Operation is not supported: {}", self.reason)
109    }
110}
111impl Error for NotSupportedError {}
112
113/// Unclassified error from the OS.
114#[derive(Debug)]
115pub struct OsError {
116    line: u32,
117    file: &'static str,
118    error: Box<dyn Error + Send + Sync + 'static>,
119}
120
121impl OsError {
122    pub fn new(
123        line: u32,
124        file: &'static str,
125        error: impl Into<Box<dyn Error + Send + Sync + 'static>>,
126    ) -> Self {
127        Self { line, file, error: error.into() }
128    }
129}
130
131impl Display for OsError {
132    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133        f.pad(&format!("os error at {}:{}: {}", self.file, self.line, self.error))
134    }
135}
136impl Error for OsError {
137    fn source(&self) -> Option<&(dyn Error + 'static)> {
138        Some(self.error.as_ref())
139    }
140}
141
142#[allow(unused_macros)]
143macro_rules! os_error {
144    ($error:expr) => {{ crate::error::OsError::new(line!(), file!(), $error) }};
145}