Skip to content

Commit

Permalink
span: improve invalid units error message
Browse files Browse the repository at this point in the history
Previously, we would report a range error when trying to use units of
days or greater in a span when adding to a `Timestamp`. But this error
doesn't really do a good job of explaining anything. It was a hold-over
from the days where I was using many different error types and it was
difficult to improve messages on an ad hoc basis. But now, we can just
write a message.

The error message doesn't explain *why* days-and-greater are banned, but
it's hard to fit all of that into an error. At some point, we just have
to rely on folks reading the docs.

Closes #90
  • Loading branch information
BurntSushi committed Aug 11, 2024
1 parent 4e61c9e commit c659069
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2722,17 +2722,22 @@ impl Span {
pub(crate) fn smallest_non_time_non_zero_unit_error(
&self,
) -> Option<Error> {
if self.days != 0 {
Some(t::SpanDays::error("days", self.days.get()))
let non_time_unit = if self.days != 0 {
Unit::Day
} else if self.weeks != 0 {
Some(t::SpanWeeks::error("weeks", self.weeks.get()))
Unit::Week
} else if self.months != 0 {
Some(t::SpanMonths::error("months", self.months.get()))
Unit::Month
} else if self.years != 0 {
Some(t::SpanYears::error("years", self.years.get()))
Unit::Year
} else {
None
}
return None;
};
Some(err!(
"operation can only be performed with units of hours \
or smaller, but found non-zero {unit} units",
unit = non_time_unit.singular(),
))
}

/// Returns the largest non-zero unit in this span.
Expand Down

0 comments on commit c659069

Please sign in to comment.