Files
rustytorch/demos/server/src/error.rs
T
2026-03-04 00:08:42 +00:00

139 lines
3.5 KiB
Rust

//! Error types for the hemodynamics server
//!
//! This module defines all error types used by the server layer.
use thiserror::Error;
/// Result type for server operations
pub type ServerResult<T> = Result<T, ServerError>;
/// Server error types
#[derive(Debug, Error)]
pub enum ServerError {
/// Simulation not initialized
#[error("Simulation not initialized. Call initialize() first.")]
NotInitialized,
/// Simulation already initialized
#[error("Simulation already initialized. Call reset() first.")]
AlreadyInitialized,
/// Invalid geometry parameters
#[error("Invalid geometry parameters: {0}")]
InvalidGeometry(String),
/// Invalid query points
#[error("Invalid query: {0}")]
InvalidQuery(String),
/// Inference failed
#[error("Inference failed: {0}")]
InferenceFailed(String),
/// Training failed
#[error("Training failed: {0}")]
TrainingFailed(String),
/// Model not trained
#[error("Model not trained. Train the model first.")]
ModelNotTrained,
/// Resource not found
#[error("Not found: {0}")]
NotFound(String),
/// Processing failed
#[error("Processing failed: {0}")]
ProcessingFailed(String),
/// Configuration error
#[error("Configuration error: {0}")]
ConfigError(String),
/// I/O error
#[error("I/O error: {0}")]
IoError(#[from] std::io::Error),
/// Serialization error
#[error("Serialization error: {0}")]
SerializationError(#[from] serde_json::Error),
/// Internal error
#[error("Internal error: {0}")]
Internal(String),
}
impl ServerError {
/// Creates a new geometry error
#[must_use]
pub fn geometry(msg: impl Into<String>) -> Self {
Self::InvalidGeometry(msg.into())
}
/// Creates a new query error
#[must_use]
pub fn query(msg: impl Into<String>) -> Self {
Self::InvalidQuery(msg.into())
}
/// Creates a new inference error
#[must_use]
pub fn inference(msg: impl Into<String>) -> Self {
Self::InferenceFailed(msg.into())
}
/// Creates a new training error
#[must_use]
pub fn training(msg: impl Into<String>) -> Self {
Self::TrainingFailed(msg.into())
}
/// Creates a new internal error
#[must_use]
pub fn internal(msg: impl Into<String>) -> Self {
Self::Internal(msg.into())
}
/// Creates a not found error
#[must_use]
pub fn not_found(msg: impl Into<String>) -> Self {
Self::NotFound(msg.into())
}
/// Creates a processing error
#[must_use]
pub fn processing(msg: impl Into<String>) -> Self {
Self::ProcessingFailed(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = ServerError::NotInitialized;
assert!(err.to_string().contains("not initialized"));
let err = ServerError::geometry("bad length");
assert!(err.to_string().contains("bad length"));
}
#[test]
fn test_error_creation() {
let err = ServerError::inference("NaN encountered");
assert!(matches!(err, ServerError::InferenceFailed(_)));
let err = ServerError::training("diverged");
assert!(matches!(err, ServerError::TrainingFailed(_)));
}
#[test]
fn test_error_from_io() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let server_err: ServerError = io_err.into();
assert!(matches!(server_err, ServerError::IoError(_)));
}
}