use crate::result::{ClassifyError, ErrorBoundaryDecision, ErrorBoundaryPolicy};
use super::{ApiProblem, IntoApiProblem, PublicError};
use crate::http::request_context::RequestContext;
#[derive(Debug, Clone, Copy)]
pub struct ApiErrorBoundary {
policy: ErrorBoundaryPolicy,
}
impl ApiErrorBoundary {
#[must_use]
pub const fn new(policy: ErrorBoundaryPolicy) -> Self {
Self { policy }
}
#[must_use]
pub const fn http_api() -> Self {
Self::new(ErrorBoundaryPolicy::http_api())
}
#[must_use]
pub const fn policy(&self) -> ErrorBoundaryPolicy {
self.policy
}
#[must_use]
pub fn decide<E>(&self, error: &E) -> ErrorBoundaryDecision
where
E: ClassifyError + ?Sized,
{
self.policy.decide(error)
}
#[must_use]
pub fn problem<E>(&self, error: &E, context: &RequestContext) -> ApiProblem
where
E: ClassifyError + ?Sized,
{
self.problem_from_decision(self.decide(error), context)
}
#[must_use]
pub fn problem_from_decision(
&self,
decision: ErrorBoundaryDecision,
context: &RequestContext,
) -> ApiProblem {
PublicError::from_descriptor(decision.into_descriptor())
.into_api_problem()
.with_request_context(context)
}
}