-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Environment
-
Elixir & Erlang versions (elixir --version): Elixir 1.5.0 & Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
-
Operating system: Mac OS X 10.11.6
Current behavior
Without realizing it, I passed in a NaiveDateTime
into DateTime.to_iso8601()
, and got back a cryptic error message.
iex(12)> DateTime.to_iso8601(NaiveDateTime.utc_now())
** (ArgumentError) DateTime.to_iso8601/2 expects format to be :extended or :basic, got: :extended
(elixir) lib/calendar/datetime.ex:345: DateTime.to_iso8601/2
This happens because the fallback pattern assumes that if no other function patterns were matched, there must be a fault with the second argument. However, in this case the second argument got the default value and the issue was with the first argument's type.
elixir/lib/elixir/lib/calendar/datetime.ex
Lines 327 to 346 in 8914d2c
def to_iso8601(datetime, format \\ :extended) | |
def to_iso8601(%{calendar: Calendar.ISO, year: year, month: month, day: day, | |
hour: hour, minute: minute, second: second, microsecond: microsecond, | |
time_zone: time_zone, zone_abbr: zone_abbr, utc_offset: utc_offset, std_offset: std_offset}, format) when format in [:extended, :basic] do | |
Calendar.ISO.datetime_to_iso8601(year, month, day, hour, minute, second, microsecond, | |
time_zone, zone_abbr, utc_offset, std_offset, format) | |
end | |
def to_iso8601(%{calendar: _, year: _, month: _, day: _, | |
hour: _, minute: _, second: _, microsecond: _, | |
time_zone: _, zone_abbr: _, utc_offset: _, std_offset: _} = datetime, format) when format in [:extended, :basic] do | |
datetime | |
|> convert!(Calendar.ISO) | |
|> to_iso8601(format) | |
end | |
def to_iso8601(_, format) do | |
raise ArgumentError, "DateTime.to_iso8601/2 expects format to be :extended or :basic, got: #{inspect format}" | |
end |
Expected behavior
It would be helpful if there was an additional function before the last one to point out that the first argument is not a DateTime
.