refactor(server,common): introduce custom error types with thiserror and miette

This commit is contained in:
2026-02-11 16:29:59 +02:00
parent cda6024062
commit 818cfd5598
10 changed files with 134 additions and 61 deletions

View File

@@ -1,5 +1,5 @@
use crate::{args::Args, error};
use common::KeyExchangeMode;
use common::{self, KeyExchangeMode};
use serde::Deserialize;
use std::{fs::read_to_string, net::SocketAddr, path::PathBuf};
@@ -23,8 +23,8 @@ pub struct Config {
/// # Errors
/// Returns an error if the file cannot be read or parsed.
pub fn load_from_file(path: &PathBuf) -> error::Result<Config> {
let content = read_to_string(path).map_err(error::Error::Io)?;
let config = toml::from_str::<Config>(&content).map_err(error::Error::Toml)?;
let content = read_to_string(path).map_err(common::Error::Io)?;
let config = toml::from_str::<Config>(&content).map_err(common::Error::Toml)?;
Ok(config)
}
@@ -42,7 +42,7 @@ pub fn load_from_cli(args: &Args) -> error::Result<Config> {
concurrency: args.concurrency,
server: args
.server
.ok_or_else(|| error::Error::config("--server ir required"))?,
.ok_or_else(|| common::Error::config("--server ir required"))?,
}],
})
}

View File

@@ -9,64 +9,20 @@ pub type Result<T> = std::result::Result<T, Error>;
/// Errors that can occur during benchmark execution.
#[derive(Debug, Error, Diagnostic)]
pub enum Error {
/// TLS configuration or handshake failure.
#[error(transparent)]
#[diagnostic(code(runner::tls_error))]
TlsConfig(#[from] rustls::Error),
/// File or network I/O error.
#[error(transparent)]
#[diagnostic(code(runner::io_error))]
Io(#[from] std::io::Error),
/// TOML configuration file parse error.
#[error(transparent)]
#[diagnostic(code(runner::toml_error))]
Toml(#[from] toml::de::Error),
/// Invalid key exchange mode string.
#[error("Invalid mode: {0}")]
#[diagnostic(code(runner::invalid_mode))]
InvalidMode(String),
/// Configuration validation or missing required fields.
#[error("Config error: {0}")]
#[diagnostic(code(runner::config_error))]
Config(String),
#[diagnostic(code(runner::common_error))]
Common(#[from] common::Error),
/// Network connection failure.
#[error("Network error: {0}")]
#[diagnostic(code(runner::network_error))]
Network(String),
/// Protocol-level error (malformed requests, unexpected responses).
#[error("Protocol error: {0}")]
#[diagnostic(code(runner::protocol_error))]
Protocol(String),
}
impl Error {
/// Create an invalid mode error.
#[inline]
pub fn invalid_mode(error: impl Into<String>) -> Self {
Self::InvalidMode(error.into())
}
/// Create a config error.
#[inline]
pub fn config(error: impl Into<String>) -> Self {
Self::Config(error.into())
}
/// Create a network error.
#[inline]
pub fn network(error: impl Into<String>) -> Self {
Self::Network(error.into())
}
/// Create a protocol error.
#[inline]
pub fn protocol(error: impl Into<String>) -> Self {
Self::Protocol(error.into())
}
}