Skip to main content

ferroui_splat/logging/
logging_exception.rs

1//! `LoggingException`.
2
3use std::error::Error;
4use std::fmt;
5
6/// The failure of the logging infrastructure (`LoggingException`): a log manager or a logger that
7/// the resolver does not have.
8///
9/// Where upstream throws it, the port panics with it as the payload
10/// (`std::panic::panic_any`), so that a caller that catches the panic can tell it apart.
11#[derive(Debug)]
12pub struct LoggingException {
13    message: String,
14    inner: Option<Box<dyn Error + Send + Sync>>,
15}
16
17impl LoggingException {
18    /// The exception with the default message (`LoggingException()`).
19    pub fn new() -> Self {
20        Self { message: "Exception of type 'LoggingException' was thrown.".to_owned(), inner: None }
21    }
22
23    /// The exception with `message` (`LoggingException(string)`).
24    pub fn with_message(message: impl Into<String>) -> Self {
25        Self { message: message.into(), inner: None }
26    }
27
28    /// The exception with `message`, caused by `inner_exception`
29    /// (`LoggingException(string, Exception)`).
30    pub fn with_inner(message: impl Into<String>, inner_exception: Box<dyn Error + Send + Sync>) -> Self {
31        Self { message: message.into(), inner: Some(inner_exception) }
32    }
33
34    /// The message (`Message`).
35    pub fn message(&self) -> &str {
36        &self.message
37    }
38
39    /// The exception that caused this one (`InnerException`).
40    pub fn inner_exception(&self) -> Option<&(dyn Error + Send + Sync + 'static)> {
41        self.inner.as_deref()
42    }
43}
44
45impl Default for LoggingException {
46    fn default() -> Self {
47        Self::new()
48    }
49}
50
51impl fmt::Display for LoggingException {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        f.write_str(&self.message)
54    }
55}
56
57impl Error for LoggingException {
58    fn source(&self) -> Option<&(dyn Error + 'static)> {
59        self.inner.as_deref().map(|inner| inner as &(dyn Error + 'static))
60    }
61}