1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 15:51:06 +01:00

feat(settings): impl Error for Error

This commit is contained in:
Rob Ede 2023-09-16 01:19:16 +01:00
parent 257871ca7a
commit 0d27e3a65a
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 33 additions and 48 deletions

View File

@ -5,6 +5,10 @@
- Rename `AtError => Error`.
- Remove `AtResult` type alias.
- Update `toml` dependency to `0.8`.
- Remove `ioe` dependency; `std::io::Error` is now used directly.
- Remove `Clone` implementation for `Error`.
- Implement `Display` for `Error`.
- Implement std's `Error` for `Error`.
- Minimum supported Rust version (MSRV) is now 1.68.
## 0.6.0

View File

@ -18,8 +18,7 @@ all-features = true
actix-http = "3"
actix-service = "2"
actix-web = "4"
ioe = "0.5"
derive_more = "0.99.7"
once_cell = "1.13"
regex = "1.5.5"
serde = { version = "1", features = ["derive"] }

View File

@ -1,18 +1,22 @@
use std::{env::VarError, io, num::ParseIntError, path::PathBuf, str::ParseBoolError};
use derive_more::{Display, Error};
use toml::de::Error as TomlError;
/// Errors that can be returned from methods in this crate.
#[derive(Debug, Clone)]
#[derive(Debug, Display, Error)]
pub enum Error {
/// Environment variable does not exists or is invalid.
#[display(fmt = "Env var error: {_0}")]
EnvVarError(VarError),
/// File already exists on disk.
FileExists(PathBuf),
#[display(fmt = "File exists: {}", "_0.display()")]
FileExists(#[error(not(source))] PathBuf),
/// Invalid value.
#[allow(missing_docs)]
#[display(fmt = "Expected {expected}, got {got} (@ {file}:{line}:{column})")]
InvalidValue {
expected: &'static str,
got: String,
@ -22,18 +26,23 @@ pub enum Error {
},
/// I/O error.
IoError(ioe::IoError),
#[display(fmt = "")]
IoError(io::Error),
/// Value is not a boolean.
#[display(fmt = "Failed to parse boolean: {_0}")]
ParseBoolError(ParseBoolError),
/// Value is not an integer.
#[display(fmt = "Failed to parse integer: {_0}")]
ParseIntError(ParseIntError),
/// Value is not an address.
ParseAddressError(String),
#[display(fmt = "Failed to parse address: {_0}")]
ParseAddressError(#[error(not(source))] String),
/// Error deserializing as TOML.
#[display(fmt = "TOML error: {_0}")]
TomlError(TomlError),
}
@ -51,12 +60,6 @@ macro_rules! InvalidValue {
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::IoError(ioe::IoError::from(err))
}
}
impl From<ioe::IoError> for Error {
fn from(err: ioe::IoError) -> Self {
Self::IoError(err)
}
}
@ -88,48 +91,27 @@ impl From<VarError> for Error {
impl From<Error> for io::Error {
fn from(err: Error) -> Self {
match err {
Error::EnvVarError(var_error) => {
let msg = format!("Env var error: {var_error}");
io::Error::new(io::ErrorKind::InvalidInput, msg)
Error::EnvVarError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()),
Error::FileExists(_) => io::Error::new(io::ErrorKind::AlreadyExists, err.to_string()),
Error::InvalidValue { .. } => {
io::Error::new(io::ErrorKind::InvalidInput, err.to_string())
}
Error::FileExists(path_buf) => {
let msg = format!("File exists: {}", path_buf.display());
io::Error::new(io::ErrorKind::AlreadyExists, msg)
Error::IoError(io_error) => io_error,
Error::ParseBoolError(_) => {
io::Error::new(io::ErrorKind::InvalidInput, err.to_string())
}
Error::InvalidValue {
expected,
ref got,
file,
line,
column,
} => {
let msg = format!("Expected {expected}, got {got} (@ {file}:{line}:{column})");
io::Error::new(io::ErrorKind::InvalidInput, msg)
Error::ParseIntError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()),
Error::ParseAddressError(_) => {
io::Error::new(io::ErrorKind::InvalidInput, err.to_string())
}
Error::IoError(io_error) => io_error.into(),
Error::ParseBoolError(parse_bool_error) => {
let msg = format!("Failed to parse boolean: {parse_bool_error}");
io::Error::new(io::ErrorKind::InvalidInput, msg)
}
Error::ParseIntError(parse_int_error) => {
let msg = format!("Failed to parse integer: {parse_int_error}");
io::Error::new(io::ErrorKind::InvalidInput, msg)
}
Error::ParseAddressError(string) => {
let msg = format!("Failed to parse address: {string}");
io::Error::new(io::ErrorKind::InvalidInput, msg)
}
Error::TomlError(toml_error) => {
let msg = format!("TOML error: {toml_error}");
io::Error::new(io::ErrorKind::InvalidInput, msg)
}
Error::TomlError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()),
}
}
}