From 45fd414f1bf0039a2ae518c665e71d40ed3307a3 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 30 Mar 2026 14:42:20 +0100 Subject: [PATCH] Use friendly error messages in Last.fm callback Closes #145 --- lib/music_library_web/controllers/last_fm_controller.ex | 7 ++++++- priv/gettext/default.pot | 5 +++++ priv/gettext/en/LC_MESSAGES/default.po | 5 +++++ .../controllers/last_fm_controller_test.exs | 6 ++++-- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/music_library_web/controllers/last_fm_controller.ex b/lib/music_library_web/controllers/last_fm_controller.ex index 092b5422..f8b752de 100644 --- a/lib/music_library_web/controllers/last_fm_controller.ex +++ b/lib/music_library_web/controllers/last_fm_controller.ex @@ -2,6 +2,7 @@ defmodule MusicLibraryWeb.LastFmController do use MusicLibraryWeb, :controller alias MusicLibrary.Secrets + alias MusicLibraryWeb.ErrorMessages def callback(conn, %{"token" => token}) do with {:ok, session} <- LastFm.get_session(token), @@ -12,7 +13,11 @@ defmodule MusicLibraryWeb.LastFmController do else {:error, reason} -> conn - |> put_toast(:error, "Failed to connect your Last.fm account: #{reason}") + |> put_toast( + :error, + gettext("Failed to connect your Last.fm account") <> + ": " <> ErrorMessages.friendly_message(reason) + ) |> redirect(to: ~p"/") end end diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index d62516cc..7ec6f5e5 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -2380,3 +2380,8 @@ msgstr "" #, elixir-autogen, elixir-format msgid "the cover image asset was not found" msgstr "" + +#: lib/music_library_web/controllers/last_fm_controller.ex +#, elixir-autogen, elixir-format +msgid "Failed to connect your Last.fm account" +msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 7429091f..399371d6 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -2380,3 +2380,8 @@ msgstr "" #, elixir-autogen, elixir-format msgid "the cover image asset was not found" msgstr "" + +#: lib/music_library_web/controllers/last_fm_controller.ex +#, elixir-autogen, elixir-format +msgid "Failed to connect your Last.fm account" +msgstr "" diff --git a/test/music_library_web/controllers/last_fm_controller_test.exs b/test/music_library_web/controllers/last_fm_controller_test.exs index 5cc109e9..9e0ec974 100644 --- a/test/music_library_web/controllers/last_fm_controller_test.exs +++ b/test/music_library_web/controllers/last_fm_controller_test.exs @@ -27,7 +27,7 @@ defmodule MusicLibraryWeb.LastFmControllerTest do @tag :logged_out @tag :capture_log - test "on error, redirects with error flash", %{conn: conn} do + test "on error, redirects with friendly error flash", %{conn: conn} do error_json = %{ "error" => 4, "message" => "Invalid authentication token" @@ -40,7 +40,9 @@ defmodule MusicLibraryWeb.LastFmControllerTest do conn = get(conn, ~p"/auth/last_fm/callback", %{"token" => "bad-token"}) assert redirected_to(conn) == "/" - assert conn.assigns.flash["error"] =~ "Failed to connect" + + assert conn.assigns.flash["error"] == + "Failed to connect your Last.fm account: Last.fm authentication failed" end end end