pub struct Error {
pub profile: Option<Profile>,
pub metadata: Option<Metadata>,
pub path: Vec<String>,
pub kind: Kind,
/* private fields */
}Expand description
An error that occured while producing data or extracting a configuration.
§Constructing Errors
An Error will generally be constructed indirectly via its implementations
of serde’s de::Error and ser::Error, that is, as a result of
serialization or deserialization errors. When implementing Provider,
however, it may be necessary to construct an Error directly.
Broadly, there are two ways to construct an Error:
-
With an error message, as
ErrorimplsFrom<String>andFrom<&str>:use figment::Error; Error::from(format!("{} is invalid", 1)); Error::from("whoops, something went wrong!"); -
With a
Kind, asErrorimplsFrom<Kind>:use figment::{error::{Error, Kind}, value::Value}; let value = Value::serialize(&100).unwrap(); if !value.as_str().is_some() { let kind = Kind::InvalidType(value.to_actual(), "string".into()); let error = Error::from(kind); }
As always, ? can be used to automatically convert into an Error using
the available From implementations:
use std::fs::File;
fn try_read() -> Result<(), figment::Error> {
let x = File::open("/tmp/foo.boo").map_err(|e| e.to_string())?;
Ok(())
}§Display
By default, Error uses all of the available information about the error,
including the Metadata, path, and profile to display a message that
resembles the following, where $ is error. for some error: Error:
$kind: `$metadata.interpolate($path)` in $($metadata.sources())*Concretely, such an error may look like:
invalid type: found sequence, expected u16: `staging.port` in TOML file Config.toml§Iterator
An Error may contain more than one error. To process all errors, iterate
over an Error:
fn with_error(error: figment::Error) {
for error in error {
println!("error: {}", error);
}
}Fields§
§profile: Option<Profile>The profile that was selected when the error occured, if any.
metadata: Option<Metadata>The metadata for the provider of the value that errored, if known.
path: Vec<String>The path to the configuration key that errored, if known.
kind: KindThe error kind.
Implementations§
Source§impl Error
impl Error
Sourcepub fn missing(&self) -> bool
pub fn missing(&self) -> bool
Returns true if the error’s kind is MissingField.
§Example
use figment::error::{Error, Kind};
let error = Error::from(Kind::MissingField("path".into()));
assert!(error.missing());Sourcepub fn with_path(self, path: &str) -> Self
pub fn with_path(self, path: &str) -> Self
Append the string path to the error’s path.
§Example
use figment::Error;
let error = Error::from("an error message").with_path("some_path");
assert_eq!(error.path, vec!["some_path"]);
let error = Error::from("an error message").with_path("some.path");
assert_eq!(error.path, vec!["some", "path"]);Sourcepub fn chain(self, error: Error) -> Self
pub fn chain(self, error: Error) -> Self
Prepends self to error and returns error.
use figment::error::Error;
let e1 = Error::from("1");
let e2 = Error::from("2");
let e3 = Error::from("3");
let error = e1.chain(e2).chain(e3);
assert_eq!(error.count(), 3);
let unchained = error.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>();
assert_eq!(unchained, vec!["3", "2", "1"]);
let e1 = Error::from("1");
let e2 = Error::from("2");
let e3 = Error::from("3");
let error = e3.chain(e2).chain(e1);
assert_eq!(error.count(), 3);
let unchained = error.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>();
assert_eq!(unchained, vec!["1", "2", "3"]);Sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Returns the number of errors represented by self.
§Example
use figment::{Figment, providers::{Format, Toml}};
figment::Jail::expect_with(|jail| {
jail.create_file("Base.toml", r#"
cat = [1
"#)?;
jail.create_file("Release.toml", r#"
cat = "
"#)?;
let figment = Figment::from(Toml::file("Base.toml"))
.merge(Toml::file("Release.toml"));
let error = figment.extract_inner::<String>("cat").unwrap_err();
assert_eq!(error.count(), 2);
Ok(())
});Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn custom<T: Display>(msg: T) -> Self
fn custom<T: Display>(msg: T) -> Self
Source§fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
Deserialize receives a type different from what it was
expecting. Read moreSource§fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
Deserialize receives a value of the right type but that
is wrong for some other reason. Read moreSource§fn invalid_length(len: usize, exp: &dyn Expected) -> Self
fn invalid_length(len: usize, exp: &dyn Expected) -> Self
Source§fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self
Deserialize enum type received a variant with an
unrecognized name.Source§fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self
Deserialize struct type received a field with an
unrecognized name.Source§fn missing_field(field: &'static str) -> Self
fn missing_field(field: &'static str) -> Self
Deserialize struct type expected to receive a required
field with a particular name but that field was not present in the
input.Source§fn duplicate_field(field: &'static str) -> Self
fn duplicate_field(field: &'static str) -> Self
Deserialize struct type received more than one of the
same field.Source§impl Error for Error
impl Error for Error
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl IntoIterator for Error
impl IntoIterator for Error
impl StructuralPartialEq for Error
Auto Trait Implementations§
impl Freeze for Error
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 208 bytes