Map Last.fm error codes to error atoms

This commit is contained in:
Claudio Ortolina
2024-12-27 19:54:10 +00:00
parent 1a0e505e51
commit db57195a0a
2 changed files with 25 additions and 10 deletions
+17 -2
View File
@@ -145,9 +145,24 @@ defmodule LastFm.APIImpl do
String.replace(url, api_key, "<redacted_api_key>")
end
defp identify_body({:ok, error = %{"error" => _error_number, "message" => _message}}) do
{:error, error}
defp identify_body({:ok, %{"error" => error_number, "message" => _message}}) do
{:error, map_error(error_number)}
end
defp identify_body(other), do: other
defp map_error(2), do: :invalid_service
defp map_error(3), do: :invalid_method
defp map_error(4), do: :authentication_failed
defp map_error(5), do: :invalid_format
defp map_error(6), do: :invalid_parameters
defp map_error(7), do: :invalid_resource
defp map_error(8), do: :operation_failed
defp map_error(9), do: :invalid_session_key
defp map_error(10), do: :invalid_api_key
defp map_error(11), do: :service_offline
defp map_error(13), do: :invalid_method_signature
defp map_error(16), do: :transient_error
defp map_error(26), do: :suspended_api_key
defp map_error(29), do: :rate_limit_exceeded
end
+8 -8
View File
@@ -22,8 +22,7 @@ defmodule MusicLibrary.Artists do
def get_artist_info(artist) do
last_fm_config = last_fm_config()
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
case last_fm_config.api.get_artist_info(
{:musicbrainz_id, artist.musicbrainz_id},
last_fm_config
@@ -31,8 +30,9 @@ defmodule MusicLibrary.Artists do
{:ok, info} ->
{:ok, info}
# TODO: remap error codes
{:error, %{"error" => 6}} ->
{:error, :invalid_parameters} ->
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
last_fm_config.api.get_artist_info({:name, artist.name}, last_fm_config)
error ->
@@ -42,8 +42,7 @@ defmodule MusicLibrary.Artists do
def get_similar_artists(artist) do
last_fm_config = last_fm_config()
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
case last_fm_config.api.get_similar_artists(
{:musicbrainz_id, artist.musicbrainz_id},
last_fm_config
@@ -51,8 +50,9 @@ defmodule MusicLibrary.Artists do
{:ok, info} ->
{:ok, info}
# TODO: remap error codes
{:error, %{"error" => 6}} ->
{:error, :invalid_parameters} ->
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
last_fm_config.api.get_similar_artists({:name, artist.name}, last_fm_config)
error ->