From c730a220232d6343e50aadc6d6c37b308215e401 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 21 Sep 2025 18:10:06 +0800 Subject: Update TcpTargetError --- crates/utils/tcp_connection/src/error.rs | 91 +++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 13 deletions(-) diff --git a/crates/utils/tcp_connection/src/error.rs b/crates/utils/tcp_connection/src/error.rs index 02f96e3..171e23d 100644 --- a/crates/utils/tcp_connection/src/error.rs +++ b/crates/utils/tcp_connection/src/error.rs @@ -1,24 +1,89 @@ -#[derive(Default, Clone, Eq, PartialEq)] -pub struct TcpTargetError { - msg: String, +use std::io; +use thiserror::Error; + +#[derive(Error, Debug, Clone)] +pub enum TcpTargetError { + #[error("I/O error: {0}")] + Io(String), + + #[error("Serialization error: {0}")] + Serialization(String), + + #[error("Cryptographic error: {0}")] + Crypto(String), + + #[error("Protocol error: {0}")] + Protocol(String), + + #[error("Authentication failed: {0}")] + Authentication(String), + + #[error("File operation error: {0}")] + File(String), + + #[error("Network error: {0}")] + Network(String), + + #[error("Invalid configuration: {0}")] + Config(String), + + #[error("Timeout: {0}")] + Timeout(String), + + #[error("Unsupported operation: {0}")] + Unsupported(String), } -impl<'a> std::fmt::Display for TcpTargetError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.msg) +impl From for TcpTargetError { + fn from(error: io::Error) -> Self { + TcpTargetError::Io(error.to_string()) } } -impl<'a> From<&'a str> for TcpTargetError { - fn from(value: &'a str) -> Self { - Self { - msg: value.to_string(), - } +impl From for TcpTargetError { + fn from(error: serde_json::Error) -> Self { + TcpTargetError::Serialization(error.to_string()) } } -impl<'a> From for TcpTargetError { +impl From<&str> for TcpTargetError { + fn from(value: &str) -> Self { + TcpTargetError::Protocol(value.to_string()) + } +} + +impl From for TcpTargetError { fn from(value: String) -> Self { - Self { msg: value } + TcpTargetError::Protocol(value) + } +} + +impl From for TcpTargetError { + fn from(error: rsa::errors::Error) -> Self { + TcpTargetError::Crypto(error.to_string()) + } +} + +impl From for TcpTargetError { + fn from(error: ed25519_dalek::SignatureError) -> Self { + TcpTargetError::Crypto(error.to_string()) + } +} + +impl From for TcpTargetError { + fn from(error: ring::error::Unspecified) -> Self { + TcpTargetError::Crypto(error.to_string()) + } +} + +impl From for TcpTargetError { + fn from(error: base64::DecodeError) -> Self { + TcpTargetError::Serialization(error.to_string()) + } +} + +impl From for TcpTargetError { + fn from(error: pem::PemError) -> Self { + TcpTargetError::Crypto(error.to_string()) } } -- cgit