use std::any::Any;
use std::ops::Deref;
use std::sync::{Arc, Mutex, MutexGuard};
use thiserror::Error;
use crate::error::error_impl::impl_into_cot_error;
#[derive(Debug, Clone, Error)]
#[error("an unexpected error occurred")]
pub struct UncaughtPanic {
payload: Arc<Mutex<Box<dyn Any + Send + 'static>>>,
}
impl_into_cot_error!(UncaughtPanic, INTERNAL_SERVER_ERROR);
impl UncaughtPanic {
#[must_use]
pub fn new(payload: Box<dyn Any + Send + 'static>) -> Self {
Self {
payload: Arc::new(Mutex::new(payload)),
}
}
#[must_use]
pub fn payload(&self) -> UncaughtPanicPayload<'_> {
let mutex_guard = self.payload.lock().expect("failed to lock panic payload");
UncaughtPanicPayload { mutex_guard }
}
}
#[derive(Debug)]
pub struct UncaughtPanicPayload<'a> {
mutex_guard: MutexGuard<'a, Box<dyn Any + Send + 'static>>,
}
impl Deref for UncaughtPanicPayload<'_> {
type Target = Box<dyn Any + Send + 'static>;
fn deref(&self) -> &Self::Target {
&self.mutex_guard
}
}