From 797f30b9a42a13a97381df71c8996631de0c3e04 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 2 Dec 2024 21:36:18 +0000 Subject: [PATCH] Tag Last.fm errors as errors Because they're always returned as 200 responses with custom error codes (sigh). --- lib/last_fm/api_impl.ex | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/last_fm/api_impl.ex b/lib/last_fm/api_impl.ex index d7dee713..8f4e287b 100644 --- a/lib/last_fm/api_impl.ex +++ b/lib/last_fm/api_impl.ex @@ -81,6 +81,7 @@ defmodule LastFm.APIImpl do case Finch.request(req, LastFm.Finch, @request_opts) do {:ok, response} when response.status == 200 -> Jason.decode(response.body) + |> identify_body() other -> other @@ -90,4 +91,10 @@ defmodule LastFm.APIImpl do defp sanitize_url(url, api_key) do String.replace(url, api_key, "") end + + defp identify_body({:ok, %{"error" => error_number, "message" => message}}) do + {:error, "Error #{error_number}: #{message}"} + end + + defp identify_body(other), do: other end