Skip to main content

tiny_proxy/
error.rs

1//! Error types for tiny-proxy
2//!
3//! This module defines custom error types using `thiserror` for better
4//! error handling and reporting throughout the application.
5
6use std::net::AddrParseError;
7use thiserror::Error;
8
9/// Main error type for the proxy application
10#[derive(Error, Debug)]
11pub enum ProxyError {
12    /// Configuration-related errors
13    #[error("Configuration error: {0}")]
14    Config(String),
15
16    /// Backend proxy errors
17    #[error("Backend error: {0}")]
18    Backend(String),
19
20    /// Parsing errors (config, directives, etc.)
21    #[error("Parse error: {0}")]
22    Parse(String),
23
24    /// I/O errors automatically converted from std::io::Error
25    #[error("I/O error: {0}")]
26    Io(#[from] std::io::Error),
27
28    /// HTTP-related errors
29    #[error("HTTP error: {0}")]
30    Http(String),
31
32    /// Network connection errors
33    #[error("Connection error: {0}")]
34    Connection(String),
35
36    /// Directive processing errors
37    #[error("Directive error: {0}")]
38    Directive(String),
39
40    /// URL parsing errors
41    #[error("Invalid URL: {0}")]
42    InvalidUrl(String),
43
44    /// Address parsing errors
45    #[error("Invalid address: {0}")]
46    Address(String),
47}
48
49/// Type alias for Result with ProxyError
50///
51/// This makes error handling more ergonomic throughout the codebase.
52pub type Result<T> = std::result::Result<T, ProxyError>;
53
54impl From<AddrParseError> for ProxyError {
55    fn from(err: AddrParseError) -> Self {
56        ProxyError::Address(err.to_string())
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_error_display() {
66        let err = ProxyError::Config("Test error".to_string());
67        assert_eq!(err.to_string(), "Configuration error: Test error");
68    }
69
70    #[test]
71    fn test_io_error_conversion() {
72        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
73        let proxy_err: ProxyError = io_err.into();
74        assert!(matches!(proxy_err, ProxyError::Io(_)));
75        assert!(proxy_err.to_string().contains("file not found"));
76    }
77
78    #[test]
79    fn test_result_type_alias() {
80        let result: Result<String> = Ok("test".to_string());
81        assert!(result.is_ok());
82
83        let result: Result<String> = Err(ProxyError::Config("error".to_string()));
84        assert!(result.is_err());
85    }
86}