Files
rustytorch/crates/models/rtx-vision/src/error.rs
T
2026-03-04 00:08:42 +00:00

113 lines
2.9 KiB
Rust

//! Error types for the rtx-vision crate
use thiserror::Error;
/// Result type for vision operations
pub type Result<T> = std::result::Result<T, VisionError>;
/// Vision-specific error types
#[derive(Debug, Error)]
pub enum VisionError {
/// Tensor operation failed
#[error("Tensor operation failed: {0}")]
Tensor(#[from] rtx_tensor::TensorError),
/// Computation operation failed
#[error("Computation failed: {0}")]
Computation(String),
/// Shape-related error
#[error("Shape error: {0}")]
Shape(String),
/// Initialization error
#[error("Initialization failed: {0}")]
Initialization(String),
/// Autograd operation failed
#[error("Autograd operation failed: {0}")]
Autograd(String),
/// Image processing error
#[error("Image processing error: {0}")]
ImageError(#[from] image::ImageError),
/// Invalid image dimensions
#[error("Invalid image dimensions: expected {expected}, got {got}")]
InvalidDimensions { expected: String, got: String },
/// Invalid patch size
#[error("Invalid patch size: {size} for image of size {width}x{height}")]
InvalidPatchSize {
size: usize,
width: usize,
height: usize,
},
/// Invalid model configuration
#[error("Invalid model configuration: {0}")]
InvalidConfig(String),
/// Unsupported operation
#[error("Unsupported operation: {0}")]
UnsupportedOperation(String),
/// Shape mismatch
#[error("Shape mismatch: expected {expected:?}, got {got:?}")]
ShapeMismatch {
expected: Vec<usize>,
got: Vec<usize>,
},
/// Invalid number of channels
#[error("Invalid number of channels: expected {expected}, got {got}")]
InvalidChannels { expected: usize, got: usize },
/// IO error
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
/// Generic error with context
#[error("{0}")]
Other(String),
/// Invalid tensor shape (legacy)
#[error("Invalid shape: {0}")]
InvalidShape(String),
/// Tensor operation failed (legacy)
#[error("Tensor error: {0}")]
TensorError(String),
}
// Add convenience constructors for common error types
impl VisionError {
/// Create a computation error
pub fn computation<S: Into<String>>(msg: S) -> Self {
VisionError::Computation(msg.into())
}
/// Create a shape error
pub fn shape<S: Into<String>>(msg: S) -> Self {
VisionError::Shape(msg.into())
}
/// Create an initialization error
pub fn initialization<S: Into<String>>(msg: S) -> Self {
VisionError::Initialization(msg.into())
}
/// Create an autograd error
pub fn autograd<S: Into<String>>(msg: S) -> Self {
VisionError::Autograd(msg.into())
}
}
// TensorError From conversion handled by #[from] attribute in enum definition
impl From<anyhow::Error> for VisionError {
fn from(err: anyhow::Error) -> Self {
VisionError::Other(err.to_string())
}
}