use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Handler not found")]
HandlerNotFound,
#[error("Can not cast to the required type '{0}'")]
CastError(String),
#[error("Handler error {0}")]
Handler(Box<dyn std::error::Error + Send + Sync>),
#[error("Resource not found")]
ResourceNotFound,
#[error("No event handler registered")]
NoEventHandlerRegistered,
#[error("Event Processing Error")]
EventProcessingError,
#[error("Event Publishing Error")]
EventPublishingError,
}
pub type Result<T> = core::result::Result<T, Error>;
impl Error {
pub fn get_handler_error<T: std::error::Error + Send + Sync + 'static>(&self) -> Option<&T> {
match self {
Error::Handler(handler_error) => handler_error.downcast_ref::<T>(),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, thiserror::Error)]
enum TestError {}
#[test]
fn test_get_handler_error_should_return_none() {
let error = Error::HandlerNotFound;
let handler_error = error.get_handler_error::<TestError>();
assert!(handler_error.is_none());
}
}