use super::ScopeRunError;
use crate::Error;
use std::fmt;
#[derive(Debug)]
pub struct ApplicationRunFailure<E> {
body: Option<E>,
scope: Option<Box<Error>>,
shutdown: Option<Box<Error>>,
}
impl<E> ApplicationRunFailure<E> {
pub(crate) fn new(scope: Option<ScopeRunError<E>>, shutdown: Option<Error>) -> Self {
let (body, scope) = match scope {
None => (None, None),
Some(ScopeRunError::Body(body)) => (Some(body), None),
Some(ScopeRunError::Runtime(scope)) => (None, Some(scope)),
Some(ScopeRunError::BodyAndRuntime { body, runtime }) => (Some(body), Some(runtime)),
};
Self {
body,
scope: scope.map(Box::new),
shutdown: shutdown.map(Box::new),
}
}
pub(crate) fn runtime(error: Error) -> Self {
match error {
Error::ConstructionFailed(failure) => {
let (construction, shutdown) = failure.into_parts();
Self::new(Some(ScopeRunError::Runtime(construction)), Some(shutdown))
}
error => Self::new(Some(ScopeRunError::Runtime(error)), None),
}
}
pub fn body(&self) -> Option<&E> {
self.body.as_ref()
}
pub fn scope(&self) -> Option<&Error> {
self.scope.as_deref()
}
pub fn shutdown(&self) -> Option<&Error> {
self.shutdown.as_deref()
}
pub fn into_parts(self) -> (Option<E>, Option<Error>, Option<Error>) {
(
self.body,
self.scope.map(|error| *error),
self.shutdown.map(|error| *error),
)
}
}
impl<E: fmt::Display> fmt::Display for ApplicationRunFailure<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("application run failed")?;
if let Some(body) = &self.body {
write!(f, "; body: {body}")?;
}
if let Some(scope) = &self.scope {
write!(f, "; scope/runtime: {scope}")?;
}
if let Some(shutdown) = &self.shutdown {
write!(f, "; shutdown: {shutdown}")?;
}
Ok(())
}
}
impl<E: std::error::Error + 'static> std::error::Error for ApplicationRunFailure<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let Some(body) = &self.body {
Some(body)
} else if let Some(scope) = self.scope.as_deref() {
Some(scope)
} else {
self.shutdown.as_deref().map(|error| error as _)
}
}
}
#[cfg(test)]
#[path = "application_run_failure_test.rs"]
mod application_run_failure_test;