Skip to main content

trojan_server/
error.rs

1//! Server error types.
2
3use trojan_auth::AuthError;
4use trojan_metrics::{
5    ERROR_AUTH, ERROR_CONFIG, ERROR_IO, ERROR_PROTOCOL, ERROR_RESOLVE, ERROR_TLS_HANDSHAKE,
6};
7use trojan_proto::{ParseError, WriteError};
8
9/// Server error type.
10#[derive(Debug, thiserror::Error)]
11pub enum ServerError {
12    #[error("io: {0}")]
13    Io(#[from] std::io::Error),
14    #[error("tls: {0}")]
15    Tls(#[from] tokio_rustls::rustls::Error),
16    #[error("auth: {0}")]
17    Auth(#[from] AuthError),
18    #[error("config: {0}")]
19    Config(String),
20    #[error("proto: {0:?}")]
21    Proto(ParseError),
22    #[error("proto write: {0:?}")]
23    ProtoWrite(WriteError),
24    #[error("resolve failed")]
25    Resolve,
26    #[error("udp payload too large")]
27    UdpPayloadTooLarge,
28    #[error("rules: {0}")]
29    Rules(String),
30}
31
32impl ServerError {
33    /// Get the error type string for metrics.
34    pub fn error_type(&self) -> &'static str {
35        match self {
36            ServerError::Io(_) => ERROR_IO,
37            ServerError::Tls(_) => ERROR_TLS_HANDSHAKE,
38            ServerError::Auth(_) => ERROR_AUTH,
39            ServerError::Config(_) | ServerError::Rules(_) => ERROR_CONFIG,
40            ServerError::Proto(_) | ServerError::ProtoWrite(_) => ERROR_PROTOCOL,
41            ServerError::Resolve => ERROR_RESOLVE,
42            ServerError::UdpPayloadTooLarge => ERROR_PROTOCOL,
43        }
44    }
45}