qubit_progress/error/
auto_reporter_error.rs1use std::any::Any;
13use std::error::Error;
14use std::fmt;
15use std::panic;
16
17use crate::EmissionError;
18
19#[derive(Debug)]
21#[non_exhaustive]
22pub enum AutoReporterError {
23 Emission(EmissionError),
25 Panicked(WorkerPanic),
27}
28
29impl From<EmissionError> for AutoReporterError {
30 fn from(error: EmissionError) -> Self {
32 Self::Emission(error)
33 }
34}
35
36impl fmt::Display for AutoReporterError {
37 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
39 match self {
40 Self::Emission(error) => error.fmt(formatter),
41 Self::Panicked(error) => error.fmt(formatter),
42 }
43 }
44}
45
46impl Error for AutoReporterError {
47 fn source(&self) -> Option<&(dyn Error + 'static)> {
49 match self {
50 Self::Emission(error) => Some(error),
51 Self::Panicked(_) => None,
52 }
53 }
54}
55
56pub struct WorkerPanic {
58 payload: Box<dyn Any + Send + 'static>,
60}
61
62impl WorkerPanic {
63 pub(crate) fn new(payload: Box<dyn Any + Send + 'static>) -> Self {
65 Self { payload }
66 }
67
68 #[must_use]
70 pub fn message(&self) -> Option<&str> {
71 self.payload
72 .downcast_ref::<String>()
73 .map(String::as_str)
74 .or_else(|| self.payload.downcast_ref::<&'static str>().copied())
75 }
76
77 #[must_use]
79 pub fn into_payload(self) -> Box<dyn Any + Send + 'static> {
80 self.payload
81 }
82
83 pub fn resume_unwind(self) -> ! {
85 panic::resume_unwind(self.into_payload())
86 }
87}
88
89impl fmt::Debug for WorkerPanic {
90 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
93 formatter
94 .debug_struct("WorkerPanic")
95 .field("message", &self.message())
96 .finish()
97 }
98}
99
100impl fmt::Display for WorkerPanic {
101 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
103 match self.message() {
104 Some(message) => write!(
105 formatter,
106 "background reporter worker panicked: {message}"
107 ),
108 None => formatter.write_str("background reporter worker panicked"),
109 }
110 }
111}
112
113impl Error for WorkerPanic {}