mirror of
https://github.com/actix/actix-extras.git
synced 2025-02-17 08:33:30 +01:00
feat(settings): impl Error for Error
This commit is contained in:
parent
257871ca7a
commit
0d27e3a65a
@ -5,6 +5,10 @@
|
|||||||
- Rename `AtError => Error`.
|
- Rename `AtError => Error`.
|
||||||
- Remove `AtResult` type alias.
|
- Remove `AtResult` type alias.
|
||||||
- Update `toml` dependency to `0.8`.
|
- 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.
|
- Minimum supported Rust version (MSRV) is now 1.68.
|
||||||
|
|
||||||
## 0.6.0
|
## 0.6.0
|
||||||
|
@ -18,8 +18,7 @@ all-features = true
|
|||||||
actix-http = "3"
|
actix-http = "3"
|
||||||
actix-service = "2"
|
actix-service = "2"
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
|
derive_more = "0.99.7"
|
||||||
ioe = "0.5"
|
|
||||||
once_cell = "1.13"
|
once_cell = "1.13"
|
||||||
regex = "1.5.5"
|
regex = "1.5.5"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
@ -1,18 +1,22 @@
|
|||||||
use std::{env::VarError, io, num::ParseIntError, path::PathBuf, str::ParseBoolError};
|
use std::{env::VarError, io, num::ParseIntError, path::PathBuf, str::ParseBoolError};
|
||||||
|
|
||||||
|
use derive_more::{Display, Error};
|
||||||
use toml::de::Error as TomlError;
|
use toml::de::Error as TomlError;
|
||||||
|
|
||||||
/// Errors that can be returned from methods in this crate.
|
/// Errors that can be returned from methods in this crate.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Display, Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// Environment variable does not exists or is invalid.
|
/// Environment variable does not exists or is invalid.
|
||||||
|
#[display(fmt = "Env var error: {_0}")]
|
||||||
EnvVarError(VarError),
|
EnvVarError(VarError),
|
||||||
|
|
||||||
/// File already exists on disk.
|
/// File already exists on disk.
|
||||||
FileExists(PathBuf),
|
#[display(fmt = "File exists: {}", "_0.display()")]
|
||||||
|
FileExists(#[error(not(source))] PathBuf),
|
||||||
|
|
||||||
/// Invalid value.
|
/// Invalid value.
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
|
#[display(fmt = "Expected {expected}, got {got} (@ {file}:{line}:{column})")]
|
||||||
InvalidValue {
|
InvalidValue {
|
||||||
expected: &'static str,
|
expected: &'static str,
|
||||||
got: String,
|
got: String,
|
||||||
@ -22,18 +26,23 @@ pub enum Error {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/// I/O error.
|
/// I/O error.
|
||||||
IoError(ioe::IoError),
|
#[display(fmt = "")]
|
||||||
|
IoError(io::Error),
|
||||||
|
|
||||||
/// Value is not a boolean.
|
/// Value is not a boolean.
|
||||||
|
#[display(fmt = "Failed to parse boolean: {_0}")]
|
||||||
ParseBoolError(ParseBoolError),
|
ParseBoolError(ParseBoolError),
|
||||||
|
|
||||||
/// Value is not an integer.
|
/// Value is not an integer.
|
||||||
|
#[display(fmt = "Failed to parse integer: {_0}")]
|
||||||
ParseIntError(ParseIntError),
|
ParseIntError(ParseIntError),
|
||||||
|
|
||||||
/// Value is not an address.
|
/// Value is not an address.
|
||||||
ParseAddressError(String),
|
#[display(fmt = "Failed to parse address: {_0}")]
|
||||||
|
ParseAddressError(#[error(not(source))] String),
|
||||||
|
|
||||||
/// Error deserializing as TOML.
|
/// Error deserializing as TOML.
|
||||||
|
#[display(fmt = "TOML error: {_0}")]
|
||||||
TomlError(TomlError),
|
TomlError(TomlError),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,12 +60,6 @@ macro_rules! InvalidValue {
|
|||||||
|
|
||||||
impl From<io::Error> for Error {
|
impl From<io::Error> for Error {
|
||||||
fn from(err: io::Error) -> Self {
|
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)
|
Self::IoError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,48 +91,27 @@ impl From<VarError> for Error {
|
|||||||
impl From<Error> for io::Error {
|
impl From<Error> for io::Error {
|
||||||
fn from(err: Error) -> Self {
|
fn from(err: Error) -> Self {
|
||||||
match err {
|
match err {
|
||||||
Error::EnvVarError(var_error) => {
|
Error::EnvVarError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()),
|
||||||
let msg = format!("Env var error: {var_error}");
|
|
||||||
io::Error::new(io::ErrorKind::InvalidInput, msg)
|
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) => {
|
Error::IoError(io_error) => io_error,
|
||||||
let msg = format!("File exists: {}", path_buf.display());
|
|
||||||
io::Error::new(io::ErrorKind::AlreadyExists, msg)
|
Error::ParseBoolError(_) => {
|
||||||
|
io::Error::new(io::ErrorKind::InvalidInput, err.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
Error::InvalidValue {
|
Error::ParseIntError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()),
|
||||||
expected,
|
|
||||||
ref got,
|
Error::ParseAddressError(_) => {
|
||||||
file,
|
io::Error::new(io::ErrorKind::InvalidInput, err.to_string())
|
||||||
line,
|
|
||||||
column,
|
|
||||||
} => {
|
|
||||||
let msg = format!("Expected {expected}, got {got} (@ {file}:{line}:{column})");
|
|
||||||
io::Error::new(io::ErrorKind::InvalidInput, msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Error::IoError(io_error) => io_error.into(),
|
Error::TomlError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()),
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user