Replace String.to_integer with safe Integer.parse
Use Integer.parse/1 instead of String.to_integer/1 on user-supplied and external API input to prevent ArgumentError crashes on non-numeric values. Adds fallback defaults or nil returns at each call site.
This commit is contained in:
+12
-4
@@ -67,10 +67,18 @@ defmodule LastFm.Artist do
|
||||
end
|
||||
|
||||
defp get_play_count(api_response) do
|
||||
if play_count = get_in(api_response, ["stats", "userplaycount"]) do
|
||||
String.to_integer(play_count)
|
||||
else
|
||||
0
|
||||
case get_in(api_response, ["stats", "userplaycount"]) do
|
||||
nil -> 0
|
||||
value -> parse_play_count(value)
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_play_count(value) when is_binary(value) do
|
||||
case Integer.parse(value) do
|
||||
{int, ""} -> int
|
||||
_ -> 0
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_play_count(_), do: 0
|
||||
end
|
||||
|
||||
@@ -73,8 +73,10 @@ defmodule LastFm.Track do
|
||||
end
|
||||
|
||||
defp parse_scrobble_at_uts(track) do
|
||||
track["date"]["uts"]
|
||||
|> String.to_integer()
|
||||
case Integer.parse(track["date"]["uts"]) do
|
||||
{int, ""} -> int
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
def changeset(track, attrs) do
|
||||
|
||||
Reference in New Issue
Block a user