use std::{error::Error as StdError, fmt, path::PathBuf};
pub type BoxedError = Box<dyn StdError + Send + Sync + 'static>;
pub type Result<T> = std::result::Result<T, Error>;
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}
#[derive(Debug)]
pub enum DType {
F32,
Usize,
Other(String),
}
#[derive(Debug)]
pub struct ValueWithDtype {
pub r#type: DType,
pub value: String,
}
#[derive(Debug, thiserror::Error)]
pub enum InvalidParameterError {
#[error("invalid value for `{name}`: expected {expected}, got {value}")]
InvalidValue {
name: String,
expected: String,
value: String,
},
#[error("value {current_value:?} is outside the range {min:?}..={max:?}")]
InvalidRange {
min: ValueWithDtype,
max: ValueWithDtype,
current_value: ValueWithDtype,
},
#[error("invalid path: {path}", path = .path.display())]
InvalidPath {
path: PathBuf,
},
}
#[derive(Debug, thiserror::Error)]
pub enum BrokenArtifact {
#[error("missing {artifact_type} artifact at {path}", path = .path.display())]
Missing {
path: PathBuf,
artifact_type: String,
},
#[error("failed to decode {artifact_type} artifact at {path}: {source}", path = .path.display())]
Decode {
path: PathBuf,
artifact_type: String,
#[source]
source: BoxedError,
},
}
#[derive(Debug)]
pub struct BrokenArtifacts {
pub broken: Vec<BrokenArtifact>,
}
impl fmt::Display for BrokenArtifacts {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, artifact) in self.broken.iter().enumerate() {
if index > 0 {
f.write_str("; ")?;
}
write!(f, "{artifact}")?;
}
Ok(())
}
}
impl StdError for BrokenArtifacts {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.broken
.first()
.map(|artifact| artifact as &(dyn StdError + 'static))
}
}
impl From<BrokenArtifact> for Error {
fn from(artifact: BrokenArtifact) -> Self {
Self::BrokenArtifacts(BrokenArtifacts {
broken: vec![artifact],
})
}
}
#[derive(Debug, thiserror::Error)]
#[error("missing {dependency_type} dependency `{name}`")]
pub struct MissingDependency {
pub name: String,
pub dependency_type: String,
}
#[derive(Debug, thiserror::Error)]
#[error("environment operation `{operation}` failed: {source}")]
pub struct EnvironmentError {
pub operation: String,
#[source]
pub source: BoxedError,
}
#[derive(Debug, thiserror::Error)]
pub enum TensorError {
#[error("shape mismatch for tensor operation `{operation}`: left {left:?}, right {right:?}")]
ShapeMismatch {
operation: String,
left: Vec<usize>,
right: Vec<usize>,
},
#[error("invalid tensor shape {shape:?}: expected {expected} values, got {actual}")]
InvalidShape {
shape: Vec<usize>,
expected: usize,
actual: usize,
},
#[error("tensor operation `{operation}` requires non-empty input")]
EmptyInput {
operation: String,
},
#[error("tensor operation `{operation}` failed: {source}")]
Operation {
operation: String,
#[source]
source: BoxedError,
},
}
impl TensorError {
pub fn operation(
operation: impl Into<String>,
source: impl StdError + Send + Sync + 'static,
) -> Self {
Self::Operation {
operation: operation.into(),
source: Box::new(source),
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("{resource} was interrupted: {details}")]
pub struct ResourceInterrupted {
pub resource: String,
pub details: String,
}
#[derive(thiserror::Error)]
pub enum Error {
#[error("invalid parameter: {0}")]
InvalidParameter(#[source] Box<InvalidParameterError>),
#[error("invalid state for `{operation}`: {details}")]
InvalidState {
operation: String,
details: String,
},
#[error("unsupported operation `{operation}`: {details}")]
Unsupported {
operation: String,
details: String,
},
#[error("broken artifacts: {0}")]
BrokenArtifacts(#[source] BrokenArtifacts),
#[error(transparent)]
MissingDependency(#[from] MissingDependency),
#[error(transparent)]
Environment(#[from] EnvironmentError),
#[error(transparent)]
Tensor(#[from] TensorError),
#[error(transparent)]
ResourceInterrupted(#[from] ResourceInterrupted),
#[error(transparent)]
Wrapped(#[from] BoxedError),
}
impl Error {
pub fn wrap(error: impl StdError + Send + Sync + 'static) -> Self {
Self::Wrapped(Box::new(error))
}
}