use crate::processed::ProcessedValidationError;
use crate::processing::contracts::state::StateError;
use crate::processing::kernels::acme::PhaseOptimizationError;
use crate::processing::kernels::asls::AslsError;
use thiserror::Error;
#[non_exhaustive]
#[derive(Clone, Debug, Error, PartialEq)]
pub enum ProcessingError {
#[error(transparent)]
NoiseEstimation(crate::processing::NusNoiseError),
#[error(transparent)]
Reconstruction(crate::processing::kernels::ist::IstError),
#[error("{phase:?} step {step_index:?} ({target:?}): {source}")]
Step {
step_index: Option<usize>,
target: Option<crate::processing::contracts::operation::OperationTarget>,
phase: ProcessingPhase,
source: Box<ProcessingError>,
},
#[error("execution cancelled")]
Cancelled,
#[error("unsupported history algorithm: {0}")]
UnsupportedHistoryVersion(String),
#[error("{operation}: {reason}")]
InvalidDatasetState {
operation: &'static str,
reason: &'static str,
},
#[error(transparent)]
LimitExceeded(#[from] crate::resource::LimitExceeded),
#[error("replay input count mismatch: expected {expected}, found {actual}")]
InputCountMismatch {
expected: usize,
actual: usize,
},
#[error("processing plan is empty")]
EmptyPlan,
#[error("invalid processing parameter: {0}")]
InvalidParameter(&'static str),
#[error("{operation} is invalid on axis {axis}: {reason}")]
InvalidState {
axis: usize,
operation: &'static str,
reason: &'static str,
},
#[error("raw-to-processed mapping failed: {0}")]
Mapping(&'static str),
#[error("dense processing does not support rank {rank}")]
UnsupportedRank {
rank: usize,
},
#[error("missing processing capability {capability} on axis {axis:?}")]
MissingCapability {
capability: &'static str,
axis: Option<usize>,
},
#[error("prepared plan input identity does not match the dataset")]
InputIdentityMismatch,
#[error("explicit delay disagrees with axis delay evidence")]
DelayEvidenceMismatch,
#[error("time-domain signal has no positive uniform dwell calibration")]
MissingTimeCalibration,
#[error("processing size computation overflow")]
SizeOverflow,
#[error("processing work exceeds the configured work limit")]
WorkLimit,
#[error("processing allocation failed")]
AllocationFailure,
#[error("processing numerical invariant was violated")]
NumericalInvariantViolation,
#[error(transparent)]
PhaseOptimization(PhaseOptimizationError),
#[error(transparent)]
Baseline(AslsError),
#[error(transparent)]
Validation(#[from] ProcessedValidationError),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProcessingPhase {
Preflight,
Execution,
Replay,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProcessingErrorCode {
MissingCapability,
InvalidState,
InvalidRequest,
ResourceLimit,
Cancelled,
UnsupportedHistoryVersion,
IdentityMismatch,
NumericalFailure,
}
impl ProcessingError {
pub fn root_cause(&self) -> &Self {
match self {
Self::Step { source, .. } => source.root_cause(),
_ => self,
}
}
pub fn into_root_cause(self) -> Self {
match self {
Self::Step { source, .. } => source.into_root_cause(),
_ => self,
}
}
pub fn step_index(&self) -> Option<usize> {
match self {
Self::Step { step_index, .. } => *step_index,
_ => None,
}
}
pub fn code(&self) -> ProcessingErrorCode {
use ProcessingErrorCode as C;
match self.root_cause() {
Self::Reconstruction(error) => match error {
crate::processing::kernels::ist::IstError::Cancelled => C::Cancelled,
crate::processing::kernels::ist::IstError::OutputLimit
| crate::processing::kernels::ist::IstError::WorkingLimit
| crate::processing::kernels::ist::IstError::WorkLimit
| crate::processing::kernels::ist::IstError::SizeOverflow
| crate::processing::kernels::ist::IstError::AllocationFailure => C::ResourceLimit,
crate::processing::kernels::ist::IstError::InvalidInput
| crate::processing::kernels::ist::IstError::InvalidOptions
| crate::processing::kernels::ist::IstError::NoiseEstimateRequired => {
C::InvalidRequest
}
_ => C::NumericalFailure,
},
Self::NoiseEstimation(
crate::processing::NusNoiseError::UnsupportedOperation { .. }
| crate::processing::NusNoiseError::InsufficientSamples,
) => C::MissingCapability,
Self::NoiseEstimation(_) => C::NumericalFailure,
Self::Cancelled => C::Cancelled,
Self::UnsupportedHistoryVersion(_) => C::UnsupportedHistoryVersion,
Self::MissingCapability { .. }
| Self::MissingTimeCalibration
| Self::UnsupportedRank { .. } => C::MissingCapability,
Self::InvalidState { .. }
| Self::InvalidDatasetState { .. }
| Self::Mapping(_)
| Self::DelayEvidenceMismatch
| Self::Validation(_) => C::InvalidState,
Self::LimitExceeded(_)
| Self::SizeOverflow
| Self::WorkLimit
| Self::AllocationFailure => C::ResourceLimit,
Self::InputIdentityMismatch | Self::InputCountMismatch { .. } => C::IdentityMismatch,
Self::NumericalInvariantViolation | Self::PhaseOptimization(_) | Self::Baseline(_) => {
C::NumericalFailure
}
_ => C::InvalidRequest,
}
}
pub(crate) fn located(
self,
step_index: Option<usize>,
target: Option<crate::processing::contracts::operation::OperationTarget>,
phase: ProcessingPhase,
) -> Self {
if matches!(self, Self::Step { .. }) {
self
} else {
Self::Step {
step_index,
target,
phase,
source: Box::new(self),
}
}
}
}
impl From<StateError> for ProcessingError {
fn from(error: StateError) -> Self {
match error {
StateError::Cancelled => Self::Cancelled,
StateError::InvalidDatasetState { operation, reason } => {
Self::InvalidDatasetState { operation, reason }
}
StateError::MissingCapability { capability, axis } => {
Self::MissingCapability { capability, axis }
}
StateError::InvalidParameter(parameter) => Self::InvalidParameter(parameter),
StateError::InvalidState {
axis,
operation,
reason,
} => Self::InvalidState {
axis,
operation,
reason,
},
StateError::Mapping(detail) => Self::Mapping(detail),
StateError::DelayEvidenceMismatch => Self::DelayEvidenceMismatch,
StateError::MissingTimeCalibration => Self::MissingTimeCalibration,
StateError::SizeOverflow => Self::SizeOverflow,
StateError::AllocationFailure => Self::AllocationFailure,
StateError::Validation(error) => Self::Validation(error),
}
}
}
impl From<crate::resource::ResourceError> for ProcessingError {
fn from(value: crate::resource::ResourceError) -> Self {
match value {
crate::resource::ResourceError::SizeOverflow => Self::SizeOverflow,
crate::resource::ResourceError::WorkLimit => Self::WorkLimit,
}
}
}
impl From<crate::processing::kernels::fft::FftError> for ProcessingError {
fn from(value: crate::processing::kernels::fft::FftError) -> Self {
match value {
crate::processing::kernels::fft::FftError::SizeOverflow => Self::SizeOverflow,
crate::processing::kernels::fft::FftError::InvalidLength => {
Self::InvalidParameter("FFT length")
}
crate::processing::kernels::fft::FftError::ScratchBound => {
Self::Mapping("FFT backend exceeded audited scratch bound")
}
}
}
}
impl From<PhaseOptimizationError> for ProcessingError {
fn from(error: PhaseOptimizationError) -> Self {
match error {
PhaseOptimizationError::Execution(e) => e.into(),
PhaseOptimizationError::WorkLimit => Self::WorkLimit,
other => Self::PhaseOptimization(other),
}
}
}
impl From<AslsError> for ProcessingError {
fn from(error: AslsError) -> Self {
match error {
AslsError::Execution(e) => e.into(),
other => Self::Baseline(other),
}
}
}
impl From<crate::execution::ExecutionError> for ProcessingError {
fn from(error: crate::execution::ExecutionError) -> Self {
match error {
crate::execution::ExecutionError::Cancelled => Self::Cancelled,
crate::execution::ExecutionError::WorkLimit => Self::WorkLimit,
crate::execution::ExecutionError::SizeOverflow => Self::SizeOverflow,
}
}
}