pub struct Error {
inner: Option<Arc<ErrorInner>>,
}Expand description
An error that can occur in this crate.
The most common type of error is a result of overflow. But other errors exist as well:
- Time zone database lookup failure.
- Configuration problem. (For example, trying to round a span with calendar units without providing a relative datetime.)
- An I/O error as a result of trying to open a time zone database from a
directory via
TimeZoneDatabase::from_dir. - Parse errors.
§Introspection is limited
Other than implementing the std::error::Error trait when the
std feature is enabled, the core::fmt::Debug trait and the
core::fmt::Display trait, this error type currently provides
very limited introspection capabilities. Simple predicates like
Error::is_range are provided, but the predicates are not
exhaustive. That is, there exist some errors that do not return
true for any of the Error::is_* predicates.
§Design
This crate follows the “One True God Error Type Pattern,” where only one error type exists for a variety of different operations. This design was chosen after attempting to provide finer grained error types. But finer grained error types proved difficult in the face of composition.
More about this design choice can be found in a GitHub issue about error types.
Fields§
§inner: Option<Arc<ErrorInner>>The internal representation of an error.
This is in an Arc to make an Error cloneable. It could otherwise
be automatically cloneable, but it embeds a std::io::Error when the
std feature is enabled, which isn’t cloneable.
This also makes clones cheap. And it also make the size of error equal
to one word (although a Box would achieve that last goal). This is
why we put the Arc here instead of on std::io::Error directly.
Implementations§
Source§impl Error
impl Error
Sourcepub fn from_args<'a>(message: Arguments<'a>) -> Error
pub fn from_args<'a>(message: Arguments<'a>) -> Error
Creates a new error value from core::fmt::Arguments.
It is expected to use format_args! from
Rust’s standard library (available in core) to create a
core::fmt::Arguments.
Callers should generally use their own error types. But in some circumstances, it can be convenient to manufacture a Jiff error value specifically.
§Core-only environments
In core-only environments without a dynamic memory allocator, error
messages may be degraded in some cases. For example, if the given
core::fmt::Arguments could not be converted to a simple borrowed
&str, then this will ignore the input given and return an “unknown”
Jiff error.
§Example
use jiff::Error;
let err = Error::from_args(format_args!("something failed"));
assert_eq!(err.to_string(), "something failed");Sourcepub fn is_range(&self) -> bool
pub fn is_range(&self) -> bool
Returns true when this error originated as a result of a value being out of Jiff’s supported range.
§Example
use jiff::civil::Date;
assert!(Date::new(2025, 2, 29).unwrap_err().is_range());
assert!("2025-02-29".parse::<Date>().unwrap_err().is_range());
assert!(Date::strptime("%Y-%m-%d", "2025-02-29").unwrap_err().is_range());Sourcepub fn is_invalid_parameter(&self) -> bool
pub fn is_invalid_parameter(&self) -> bool
Returns true when this error originated as a result of an invalid configuration of parameters to a function call.
This particular error category is somewhat nebulous, but it’s generally meant to cover errors that could have been statically prevented by Jiff with more types in its API. Instead, a smaller API is preferred.
§Example: invalid rounding options
use jiff::{SpanRound, ToSpan, Unit};
let span = 44.seconds();
let err = span.round(
SpanRound::new().smallest(Unit::Second).increment(45),
).unwrap_err();
// Rounding increments for seconds must divide evenly into `60`.
// But `45` does not. Thus, this is a "configuration" error.
assert!(err.is_invalid_parameter());§Example: invalid units
One cannot round a span between dates to units less than days:
use jiff::{civil::date, Unit};
let date1 = date(2025, 3, 18);
let date2 = date(2025, 12, 21);
let err = date1.until((Unit::Hour, date2)).unwrap_err();
assert!(err.is_invalid_parameter());Similarly, one cannot round a span between times to units greater than hours:
use jiff::{civil::time, Unit};
let time1 = time(9, 39, 0, 0);
let time2 = time(17, 0, 0, 0);
let err = time1.until((Unit::Day, time2)).unwrap_err();
assert!(err.is_invalid_parameter());Sourcepub fn is_crate_feature(&self) -> bool
pub fn is_crate_feature(&self) -> bool
Source§impl Error
impl Error
pub(crate) fn bounds(err: BoundsError) -> Error
pub(crate) fn special_bounds(err: SpecialBoundsError) -> Error
Sourcepub(crate) fn itime_range(err: RangeError) -> Error
pub(crate) fn itime_range(err: RangeError) -> Error
Creates a new error from the special “shared” error type.
Sourcepub(crate) fn tzif(err: TzifError) -> Error
pub(crate) fn tzif(err: TzifError) -> Error
Creates a new error from the special TZif error type.
Sourcepub(crate) fn posix_tz(err: PosixTimeZoneError) -> Error
pub(crate) fn posix_tz(err: PosixTimeZoneError) -> Error
Creates a new error from the special PosixTimeZoneError type.
Sourcepub(crate) fn io(err: Error) -> Error
pub(crate) fn io(err: Error) -> Error
A convenience constructor for building an I/O error.
This returns an error that is just a simple wrapper around the
std::io::Error type. In general, callers should always attach some
kind of context to this error (like a file path).
This is only available when the std feature is enabled.
pub(crate) fn context(self, consequent: impl IntoError) -> Error
fn context_impl(self, _consequent: Error) -> Error
Sourcefn chain(&self) -> impl Iterator<Item = &Error>
fn chain(&self) -> impl Iterator<Item = &Error>
Returns a chain of error values.
This starts with the most recent error added to the chain. That is, the highest level context. The last error in the chain is always the “root” cause. That is, the error closest to the point where something has gone wrong.
The iterator returned is guaranteed to yield at least one error.
Trait Implementations§
Source§impl Error for Error
Available on crate feature std only.
impl Error for Error
std only.