Use friendly error messages in Last.fm callback

Closes #145
This commit is contained in:
Claudio Ortolina
2026-03-30 14:42:20 +01:00
parent b0018db89d
commit 45fd414f1b
4 changed files with 20 additions and 3 deletions
@@ -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
+5
View File
@@ -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 ""
+5
View File
@@ -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 ""
@@ -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