Skip to content

Commit

Permalink
Update Kino.Input.{utc_datetime,utc_time,date} error messages
Browse files Browse the repository at this point in the history
For clarity, make the messages more consistent and always include the
argument value that caused the error, making debugging easier.

Replace "less than" and "greater than" language for dates with "before"
and "after". The re-wording also pays closer attention to the three
possible cases: before, equal, or after.
  • Loading branch information
rhcarvalho committed Sep 10, 2023
1 parent 9b75abd commit b8a02c3
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/kino/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ defmodule Kino.Input do

if min && max && DateTime.compare(min, max) == :gt do
raise ArgumentError,
"expected :min to be less than :max, got: #{inspect(min)} and #{inspect(max)}"
"expected a non-empty range, but :min (#{inspect(min)}) is after :max (#{inspect(max)})"
end

assert_default_value!(
Expand All @@ -287,11 +287,13 @@ defmodule Kino.Input do
)

if min && default && DateTime.compare(default, min) == :lt do
raise ArgumentError, "expected :default to be bigger than :min, got: #{inspect(default)} "
raise ArgumentError,
"invalid :default, #{inspect(default)} is before :min (#{inspect(min)})"
end

if max && default && DateTime.compare(default, max) == :gt do
raise ArgumentError, "expected :default to be smaller than :max, got: #{inspect(default)}"
raise ArgumentError,
"invalid :default, #{inspect(default)} is after :max (#{inspect(max)})"
end

new(%{
Expand Down Expand Up @@ -333,17 +335,19 @@ defmodule Kino.Input do

if min && max && Time.compare(min, max) == :gt do
raise ArgumentError,
"expected :min to be less than :max, got: #{inspect(min)} and #{inspect(max)}"
"expected a non-empty range, but :min (#{inspect(min)}) is after :max (#{inspect(max)})"
end

assert_default_value!(default, "be %Time{} or nil", &(is_struct(&1, Time) or &1 == nil))

if min && default && Time.compare(default, min) == :lt do
raise ArgumentError, "expected :default to be bigger than :min, got: #{inspect(default)}"
raise ArgumentError,
"invalid :default, #{inspect(default)} is before :min (#{inspect(min)})"
end

if max && default && Time.compare(default, max) == :gt do
raise ArgumentError, "expected :default to be smaller than :max, got: #{inspect(default)}"
raise ArgumentError,
"invalid :default, #{inspect(default)} is after :max (#{inspect(max)})"
end

new(%{
Expand Down Expand Up @@ -384,17 +388,19 @@ defmodule Kino.Input do

if min && max && Date.compare(min, max) == :gt do
raise ArgumentError,
"expected :min to be less than :max, got: #{inspect(min)} and #{inspect(max)}"
"expected a non-empty range, but :min (#{inspect(min)}) is after :max (#{inspect(max)})"
end

assert_default_value!(default, "be %Date{} or nil", &(is_struct(&1, Date) or &1 == nil))

if min && default && Date.compare(default, min) == :lt do
raise ArgumentError, "expected :default to be bigger than :min, got: #{inspect(default)}"
raise ArgumentError,
"invalid :default, #{inspect(default)} is before :min (#{inspect(min)})"
end

if max && default && Date.compare(default, max) == :gt do
raise ArgumentError, "expected :default to be smaller than :max, got: #{inspect(default)}"
raise ArgumentError,
"invalid :default, #{inspect(default)} is after :max (#{inspect(max)})"
end

new(%{
Expand Down

0 comments on commit b8a02c3

Please sign in to comment.