Skip to main content

map_error

Function map_error 

Source
pub fn map_error(op: Op, error: Error) -> Error
Expand description

Rebuild tenferro’s error from a tlinalg (faer route) failure.

This is the translation tenferro’s own linalg entries apply to every tlinalg failure. A caller that runs tlinalg factorizations itself, for example on the bundle tenferro_cpu::CpuEngine::with_exec lends, calls it to report exactly the error tenferro would have reported for the same failure. The types are those of the tlinalg build this crate links, so a caller names them through a tlinalg dependency of the same semver-compatible version.

§Contract

The mapping is total and never panics. It is decided in two steps:

  1. Operation name. Every error it builds names op.as_str() from the op argument. The op field stored inside error is not consulted, so when the two disagree the argument wins. The only place the inner op survives is the typed source of InsufficientWorkspace, which is the provider error itself.
  2. Variant. One match on the tlinalg::Error variant. The arms are disjoint, so their order does not change the result; the catch-all arm is last and fires only for a variant no arm names.
tlinalg::Errortenferro errorErrorKindDowncastable source
NonConvergenceExtension (linalg family)NumericalFailureError::NonConvergence { op }
NonFinite { role }Extension (linalg family)NumericalFailureError::NonFinite { op, role: role.as_str() }
SingularExtension (linalg family)NumericalFailureError::Singular { op }
InvalidArgument { role, detail }ValidationValidation(InvalidArgument)ValidationError::InvalidArgument { argument: role, message: detail }
InvalidWorkspace { library, routine, detail }BackendSourceBackendFailurecrate-private; library, routine and detail are in the message
Inconsistent { detail }Internal("{op}: {detail}")Internalnone
InsufficientWorkspaceRuntimeStateSourceRuntimeStatethe tlinalg::Error itself, op, needed and have included
any other variantInternal("{op}: unmapped linalg provider error {error:?}")Internalnone

The last row is the case that cannot be represented. tlinalg::Error is #[non_exhaustive], so a later tlinalg may add a variant this crate does not know. The mapping does not guess a kind for it: it returns the generic Internal variant with the provider error’s Debug text, so the failure stays visible and is never misclassified as a numerical or caller error. Against the tlinalg this crate links, every variant has its own row and the last row is unreachable.

§Examples

use std::error::Error as _;
use tenferro_tensor::ErrorKind;

let error = tenferro_linalg::error::map_error(
    tlinalg::Op::Svd,
    tlinalg::Error::NonConvergence { op: tlinalg::Op::Svd },
);
assert_eq!(error.kind(), ErrorKind::NumericalFailure);
assert!(matches!(
    error.source().and_then(|source| source.downcast_ref()),
    Some(tenferro_linalg::Error::NonConvergence { op: "svd" })
));