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:
Claudio Ortolina
2026-03-05 14:27:37 +00:00
parent 23c478fe4c
commit eb8a00ab54
6 changed files with 39 additions and 19 deletions
+12 -4
View File
@@ -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
+4 -2
View File
@@ -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