mirror of
https://github.com/kristoferssolo/tls-pq-bench.git
synced 2026-03-22 00:36:21 +00:00
refactor(server,common): introduce custom error types with thiserror and miette
This commit is contained in:
@@ -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"))?,
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user