Skip to main content

trojan_server/
error.rs

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