Struct rocket_db_pools::figment::error::Error
[−]pub struct Error {
pub profile: Option<Profile>,
pub metadata: Option<Metadata>,
pub path: Vec<String, Global>,
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
:
-
Via an error message, since
Error
implsFrom<String>
:use figment::Error; Error::from("whoops, something went wrong!".to_string());
-
Via a
Kind
, sinceError
implsFrom<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, Global>
The path to the configuration key that errored, if known.
kind: Kind
The error kind.
Implementations
impl Error
impl Error
pub 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());
pub fn with_path(self, path: &str) -> Error
pub fn with_path(self, path: &str) -> Error
Append the string path
to the error’s path.
Example
use figment::Error;
let error = Error::from("an error message".to_string())
.with_path("some_path");
pub 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
impl Error for Error
impl Error for Error
1.30.0 · sourcefn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
The lower-level source of this error, if any. Read more
sourcefn backtrace(&self) -> Option<&Backtrace>
fn backtrace(&self) -> Option<&Backtrace>
backtrace
)Returns a stack backtrace, if available, of where this error occurred. Read more
1.0.0 · sourcefn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
impl Error for Error
impl Error for Error
fn custom<T>(msg: T) -> Error where
T: Display,
fn custom<T>(msg: T) -> Error where
T: Display,
Raised when there is general error when deserializing a type. Read more
fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Error
fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Error
Raised when a Deserialize
receives a type different from what it was
expecting. Read more
fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Error
fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Error
Raised when a Deserialize
receives a value of the right type but that
is wrong for some other reason. Read more
fn invalid_length(len: usize, exp: &dyn Expected) -> Error
fn invalid_length(len: usize, exp: &dyn Expected) -> Error
Raised when deserializing a sequence or map and the input data contains too many or too few elements. Read more
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Error
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Error
Raised when a Deserialize
enum type received a variant with an
unrecognized name. Read more
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Error
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Error
Raised when a Deserialize
struct type received a field with an
unrecognized name. Read more
fn missing_field(field: &'static str) -> Error
fn missing_field(field: &'static str) -> Error
Raised when a Deserialize
struct type expected to receive a required
field with a particular name but that field was not present in the
input. Read more
fn duplicate_field(field: &'static str) -> Error
fn duplicate_field(field: &'static str) -> Error
Raised when a Deserialize
struct type received more than one of the
same field. Read more
impl IntoIterator for Error
impl IntoIterator for Error
impl StructuralPartialEq for Error
Auto Trait Implementations
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more