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}
29
30impl ServerError {
31    /// Get the error type string for metrics.
32    pub fn error_type(&self) -> &'static str {
33        match self {
34            ServerError::Io(_) => ERROR_IO,
35            ServerError::Tls(_) => ERROR_TLS_HANDSHAKE,
36            ServerError::Auth(_) => ERROR_AUTH,
37            ServerError::Config(_) => ERROR_CONFIG,
38            ServerError::Proto(_) | ServerError::ProtoWrite(_) => ERROR_PROTOCOL,
39            ServerError::Resolve => ERROR_RESOLVE,
40            ServerError::UdpPayloadTooLarge => ERROR_PROTOCOL,
41        }
42    }
43}