pub enum TorshError {
Show 37 variants
Shape(ShapeError),
Index(IndexError),
General(GeneralError),
WithContext {
message: String,
error_category: ErrorCategory,
severity: ErrorSeverity,
debug_context: Box<ErrorDebugContext>,
source: Option<Box<TorshError>>,
},
ShapeMismatch {
expected: Vec<usize>,
got: Vec<usize>,
},
BroadcastError {
shape1: Vec<usize>,
shape2: Vec<usize>,
},
IndexOutOfBounds {
index: usize,
size: usize,
},
InvalidArgument(String),
IoError(String),
DeviceMismatch,
NotImplemented(String),
SynchronizationError(String),
AllocationError(String),
InvalidOperation(String),
ConversionError(String),
BackendError(String),
InvalidShape(String),
RuntimeError(String),
DeviceError(String),
ConfigError(String),
InvalidState(String),
UnsupportedOperation {
op: String,
dtype: String,
},
AutogradError(String),
ComputeError(String),
SerializationError(String),
IndexError {
index: usize,
size: usize,
},
InvalidDimension {
dim: usize,
ndim: usize,
},
IterationError(String),
Other(String),
Context {
message: String,
},
InvalidDevice {
device_id: usize,
},
Backend(String),
InvalidValue(String),
Memory {
message: String,
},
CudnnError(String),
Unimplemented(String),
InitializationError(String),
}Expand description
Main ToRSh error enum - unified interface to all error types
Variants§
Shape(ShapeError)
Index(IndexError)
General(GeneralError)
WithContext
Fields
error_category: ErrorCategoryseverity: ErrorSeveritydebug_context: Box<ErrorDebugContext>source: Option<Box<TorshError>>ShapeMismatch
BroadcastError
IndexOutOfBounds
InvalidArgument(String)
IoError(String)
DeviceMismatch
NotImplemented(String)
SynchronizationError(String)
AllocationError(String)
InvalidOperation(String)
ConversionError(String)
BackendError(String)
InvalidShape(String)
RuntimeError(String)
DeviceError(String)
ConfigError(String)
InvalidState(String)
UnsupportedOperation
AutogradError(String)
ComputeError(String)
SerializationError(String)
IndexError
InvalidDimension
IterationError(String)
Other(String)
Context
InvalidDevice
Backend(String)
InvalidValue(String)
Memory
CudnnError(String)
Unimplemented(String)
InitializationError(String)
Implementations§
Source§impl TorshError
impl TorshError
Sourcepub fn shape_mismatch(expected: &[usize], got: &[usize]) -> TorshError
pub fn shape_mismatch(expected: &[usize], got: &[usize]) -> TorshError
Create a shape mismatch error (backward compatibility)
Sourcepub fn dimension_error(msg: &str, operation: &str) -> TorshError
pub fn dimension_error(msg: &str, operation: &str) -> TorshError
Create a dimension error during operation
Sourcepub fn index_error(index: usize, size: usize) -> TorshError
pub fn index_error(index: usize, size: usize) -> TorshError
Create an index error
Sourcepub fn type_mismatch(expected: &str, actual: &str) -> TorshError
pub fn type_mismatch(expected: &str, actual: &str) -> TorshError
Create a type mismatch error
Sourcepub fn dimension_error_with_context(msg: &str, operation: &str) -> TorshError
pub fn dimension_error_with_context(msg: &str, operation: &str) -> TorshError
Create a dimension error with context (backward compatibility)
Sourcepub fn synchronization_error(msg: &str) -> TorshError
pub fn synchronization_error(msg: &str) -> TorshError
Create a synchronization error (backward compatibility)
Sourcepub fn allocation_error(msg: &str) -> TorshError
pub fn allocation_error(msg: &str) -> TorshError
Create an allocation error (backward compatibility)
Sourcepub fn invalid_operation(msg: &str) -> TorshError
pub fn invalid_operation(msg: &str) -> TorshError
Create an invalid operation error (backward compatibility)
Sourcepub fn conversion_error(msg: &str) -> TorshError
pub fn conversion_error(msg: &str) -> TorshError
Create a conversion error (backward compatibility)
Sourcepub fn invalid_argument_with_context(msg: &str, context: &str) -> TorshError
pub fn invalid_argument_with_context(msg: &str, context: &str) -> TorshError
Create an invalid argument error with context (backward compatibility)
Sourcepub fn config_error_with_context(msg: &str, context: &str) -> TorshError
pub fn config_error_with_context(msg: &str, context: &str) -> TorshError
Create a config error with context (backward compatibility)
Sourcepub fn dimension_error_simple(msg: String) -> TorshError
pub fn dimension_error_simple(msg: String) -> TorshError
Create a dimension error (backward compatibility)
Sourcepub fn shape_mismatch_formatted(expected: &str, got: &str) -> TorshError
pub fn shape_mismatch_formatted(expected: &str, got: &str) -> TorshError
Create a formatted shape mismatch error (backward compatibility)
Sourcepub fn operation_error(msg: &str) -> TorshError
pub fn operation_error(msg: &str) -> TorshError
Create an operation error (backward compatibility)
Sourcepub fn wrap_with_location(self, location: String) -> TorshError
pub fn wrap_with_location(self, location: String) -> TorshError
Wrap an error with location information (backward compatibility)
Sourcepub fn category(&self) -> ErrorCategory
pub fn category(&self) -> ErrorCategory
Get the error category
Sourcepub fn severity(&self) -> ErrorSeverity
pub fn severity(&self) -> ErrorSeverity
Get the error severity
Sourcepub fn with_context(self, message: &str) -> TorshError
pub fn with_context(self, message: &str) -> TorshError
Add minimal context to an error (lightweight, no backtrace)
Use this for performance-critical paths where error context is helpful but backtrace overhead is not justified.
§Example
use torsh_core::error::{TorshError, Result};
fn tensor_operation() -> Result<()> {
let error = TorshError::InvalidShape("invalid dimensions".to_string())
.with_context("during tensor reshape");
Err(error)
}Sourcepub fn with_rich_context(self, message: &str) -> TorshError
pub fn with_rich_context(self, message: &str) -> TorshError
Add rich context to an error (includes full backtrace)
Use this for debugging and development environments where detailed error information is valuable. Captures full backtrace and thread information.
§Example
use torsh_core::error::{TorshError, Result};
fn critical_operation() -> Result<()> {
let error = TorshError::InvalidShape("invalid dimensions".to_string())
.with_rich_context("during critical tensor operation");
Err(error)
}Sourcepub fn with_metadata(self, message: &str) -> TorshError
pub fn with_metadata(self, message: &str) -> TorshError
Add context with custom metadata (minimal backtrace)
Use this to add structured metadata without the overhead of a full backtrace. Ideal for operation tracking and debugging.
§Example
use torsh_core::error::{TorshError, Result};
fn tensor_add(shape1: &[usize], shape2: &[usize]) -> Result<()> {
let error = TorshError::InvalidShape("incompatible shapes".to_string())
.with_metadata("during tensor addition")
.add_metadata("shape1", &format!("{:?}", shape1))
.add_metadata("shape2", &format!("{:?}", shape2));
Err(error)
}Sourcepub fn add_metadata(self, key: &str, value: &str) -> TorshError
pub fn add_metadata(self, key: &str, value: &str) -> TorshError
Add metadata to an existing error
This method allows adding key-value metadata to enrich error context
without creating a new error wrapper. If the error is not already
a WithContext variant, it will be converted to one.
§Example
use torsh_core::error::{TorshError, Result};
fn process_tensor(name: &str, size: usize) -> Result<()> {
let error = TorshError::AllocationError("out of memory".to_string())
.add_metadata("tensor_name", name)
.add_metadata("requested_size", &size.to_string());
Err(error)
}Sourcepub fn add_shape_metadata(self, key: &str, shape: &[usize]) -> TorshError
pub fn add_shape_metadata(self, key: &str, shape: &[usize]) -> TorshError
Add shape information as metadata
Convenience method for adding tensor shape information to errors.
§Example
use torsh_core::error::{TorshError, Result};
fn validate_shape(actual: &[usize], expected: &[usize]) -> Result<()> {
let error = TorshError::shape_mismatch(expected, actual)
.add_shape_metadata("actual_shape", actual)
.add_shape_metadata("expected_shape", expected);
Err(error)
}Sourcepub fn with_operation(self, operation: &str) -> TorshError
pub fn with_operation(self, operation: &str) -> TorshError
Add operation name as metadata
Convenience method for tracking which operation caused the error.
§Example
use torsh_core::error::{TorshError, Result};
fn matmul(a_shape: &[usize], b_shape: &[usize]) -> Result<()> {
let error = TorshError::shape_mismatch(a_shape, b_shape)
.with_operation("matmul")
.add_shape_metadata("lhs_shape", a_shape)
.add_shape_metadata("rhs_shape", b_shape);
Err(error)
}Sourcepub fn with_device(self, device_id: usize) -> TorshError
pub fn with_device(self, device_id: usize) -> TorshError
Add device information as metadata
Convenience method for tracking device-related errors.
§Example
use torsh_core::error::{TorshError, Result};
fn allocate_on_device(device_id: usize) -> Result<()> {
let error = TorshError::DeviceError("allocation failed".to_string())
.with_device(device_id)
.add_metadata("allocation_type", "tensor");
Err(error)
}Sourcepub fn with_dtype(self, dtype: &str) -> TorshError
pub fn with_dtype(self, dtype: &str) -> TorshError
Add dtype information as metadata
Convenience method for tracking data type-related errors.
§Example
use torsh_core::error::{TorshError, Result};
fn convert_dtype(from: &str, to: &str) -> Result<()> {
let error = TorshError::ConversionError("unsupported conversion".to_string())
.add_metadata("from_dtype", from)
.add_metadata("to_dtype", to);
Err(error)
}Sourcepub fn metadata(&self) -> HashMap<String, String>
pub fn metadata(&self) -> HashMap<String, String>
Get all metadata from the error
Returns an empty map if the error doesn’t have metadata.
Sourcepub fn debug_context(&self) -> Option<&ErrorDebugContext>
pub fn debug_context(&self) -> Option<&ErrorDebugContext>
Get the error’s debug context if available
Returns None if the error is not a WithContext variant.
Sourcepub fn format_debug(&self) -> String
pub fn format_debug(&self) -> String
Format the error with full debug information
This includes metadata, backtrace, and thread information when available.
Trait Implementations§
Source§impl Clone for TorshError
impl Clone for TorshError
Source§fn clone(&self) -> TorshError
fn clone(&self) -> TorshError
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TorshError
impl Debug for TorshError
Source§impl Display for TorshError
impl Display for TorshError
Source§impl Error for TorshError
impl Error for TorshError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<Error> for TorshError
impl From<Error> for TorshError
Source§fn from(err: Error) -> TorshError
fn from(err: Error) -> TorshError
Source§impl From<Error> for TorshError
Available on crate feature serialize only.
impl From<Error> for TorshError
serialize only.Source§fn from(err: Error) -> TorshError
fn from(err: Error) -> TorshError
Source§impl From<GeneralError> for TorshError
impl From<GeneralError> for TorshError
Source§fn from(source: GeneralError) -> TorshError
fn from(source: GeneralError) -> TorshError
Source§impl From<IndexError> for TorshError
impl From<IndexError> for TorshError
Source§fn from(source: IndexError) -> TorshError
fn from(source: IndexError) -> TorshError
Source§impl From<ParseFloatError> for TorshError
impl From<ParseFloatError> for TorshError
Source§fn from(err: ParseFloatError) -> TorshError
fn from(err: ParseFloatError) -> TorshError
Source§impl From<ParseIntError> for TorshError
impl From<ParseIntError> for TorshError
Source§fn from(err: ParseIntError) -> TorshError
fn from(err: ParseIntError) -> TorshError
Source§impl<T> From<PoisonError<T>> for TorshError
impl<T> From<PoisonError<T>> for TorshError
Source§fn from(err: PoisonError<T>) -> TorshError
fn from(err: PoisonError<T>) -> TorshError
Source§impl From<ShapeError> for TorshError
impl From<ShapeError> for TorshError
Source§fn from(source: ShapeError) -> TorshError
fn from(source: ShapeError) -> TorshError
Source§impl From<TryFromIntError> for TorshError
impl From<TryFromIntError> for TorshError
Source§fn from(err: TryFromIntError) -> TorshError
fn from(err: TryFromIntError) -> TorshError
Auto Trait Implementations§
impl Freeze for TorshError
impl RefUnwindSafe for TorshError
impl Send for TorshError
impl Sync for TorshError
impl Unpin for TorshError
impl UnsafeUnpin for TorshError
impl UnwindSafe for TorshError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more