viva_tensor/core/error
Centralized error types for tensor operations.
Philosophy: fail fast, fail loud, fail informatively.
Each error variant carries enough context to debug the issue without printf-debugging. ShapeMismatch tells you both shapes. IndexOutOfBounds tells you the index AND the size. No more “index out of bounds” with no context.
Why a single error type instead of operation-specific ones?
- Simpler API (one Result type everywhere)
- Easy to convert to user-facing messages
- Pattern matching still works for specific handling
Types
All the ways tensor operations can fail. Tried to keep it minimal but expressive.
pub type TensorError {
ShapeMismatch(expected: List(Int), got: List(Int))
InvalidShape(reason: String)
DimensionError(reason: String)
BroadcastError(shape_a: List(Int), shape_b: List(Int))
IndexOutOfBounds(index: Int, size: Int)
DtypeError(reason: String)
}
Constructors
-
ShapeMismatch(expected: List(Int), got: List(Int))Shape mismatch between two tensors.
Example
Trying to add [2, 3] tensor with [4, 5] tensor.
-
InvalidShape(reason: String)Invalid shape specification.
Example
Data size doesn’t match shape dimensions.
-
DimensionError(reason: String)Dimension-related error (axis out of bounds, etc.).
Example
Accessing axis 3 on a 2D tensor.
-
BroadcastError(shape_a: List(Int), shape_b: List(Int))Broadcasting incompatibility.
Example
Cannot broadcast [2, 3] with [4, 5].
-
IndexOutOfBounds(index: Int, size: Int)Index out of bounds.
Example
Accessing index 10 in tensor of size 5.
-
DtypeError(reason: String)Invalid dtype for operation.
Example
Using INT8 operation on Float32 tensor.
Values
pub fn shape_to_string(shape: List(Int)) -> String
Pretty-print a shape like [2, 3, 4].
pub fn to_string(error: TensorError) -> String
Human-readable error message. Useful for debugging.