modelexpress_client/
error.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use thiserror::Error;
5
6/// Client-specific error types
7#[derive(Debug, Error)]
8pub enum ClientError {
9    #[error("Failed to connect to server: {0}")]
10    ConnectionFailed(String),
11
12    #[error("Request timed out: {0}")]
13    Timeout(String),
14
15    #[error("Common error: {0}")]
16    Common(#[from] modelexpress_common::Error),
17
18    #[error("Invalid configuration: {0}")]
19    Config(String),
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn test_client_error_connection_failed() {
28        let error = ClientError::ConnectionFailed("Connection refused".to_string());
29        assert!(error.to_string().contains("Failed to connect to server"));
30        assert!(error.to_string().contains("Connection refused"));
31    }
32
33    #[test]
34    fn test_client_error_timeout() {
35        let error = ClientError::Timeout("Request took too long".to_string());
36        assert!(error.to_string().contains("Request timed out"));
37        assert!(error.to_string().contains("Request took too long"));
38    }
39
40    #[test]
41    fn test_client_error_config() {
42        let error = ClientError::Config("Invalid endpoint format".to_string());
43        assert!(error.to_string().contains("Invalid configuration"));
44        assert!(error.to_string().contains("Invalid endpoint format"));
45    }
46
47    #[test]
48    fn test_client_error_from_common_error() {
49        let common_error = modelexpress_common::Error::Network("Network issue".to_string());
50        let client_error: ClientError = common_error.into();
51
52        match client_error {
53            ClientError::Common(_) => {
54                assert!(client_error.to_string().contains("Common error"));
55            }
56            _ => panic!("Expected Common error variant"),
57        }
58    }
59
60    #[test]
61    fn test_all_error_variants() {
62        let errors = vec![
63            ClientError::ConnectionFailed("test".to_string()),
64            ClientError::Timeout("test".to_string()),
65            ClientError::Config("test".to_string()),
66            ClientError::Common(modelexpress_common::Error::Network("test".to_string())),
67        ];
68
69        for error in errors {
70            // Each error should have a meaningful string representation
71            let error_str = error.to_string();
72            assert!(!error_str.is_empty());
73            assert!(error_str.len() > 5); // Should be more than just "test"
74        }
75    }
76}