ML-21: Classify API errors as transient vs permanent
This commit is contained in:
+25
-7
@@ -3,11 +3,13 @@ defmodule Discogs.API do
|
||||
Interface to the Discogs API.
|
||||
"""
|
||||
|
||||
alias Discogs.API.ErrorResponse
|
||||
alias Req.Request
|
||||
|
||||
require Logger
|
||||
|
||||
@spec get_artist(integer() | String.t(), Discogs.Config.t()) :: {:ok, map()} | {:error, term()}
|
||||
@spec get_artist(integer() | String.t(), Discogs.Config.t()) ::
|
||||
{:ok, map()} | {:error, ErrorResponse.t() | Exception.t()}
|
||||
def get_artist(id, config) do
|
||||
config
|
||||
|> new_request()
|
||||
@@ -41,17 +43,20 @@ defmodule Discogs.API do
|
||||
|> Request.merge_options(config.req_options)
|
||||
|> Req.RateLimiter.attach(name: :discogs, cooldown: config.api_cooldown)
|
||||
|> Request.append_request_steps(log_attempt: &log_attempt/1)
|
||||
|> Request.append_response_steps(log_error: &log_error/1)
|
||||
|> Request.append_response_steps(parse_error: &parse_error/1)
|
||||
end
|
||||
|
||||
defp get_request(request) do
|
||||
case Req.get(request) do
|
||||
{:ok, response} when response.status == 200 ->
|
||||
{:ok, response.body}
|
||||
{:ok, %{status: status, body: body}} when status in 200..299 ->
|
||||
{:ok, body}
|
||||
|
||||
# all non-success responses can be treated as errors
|
||||
{:ok, response} ->
|
||||
{:error, response.body}
|
||||
{:ok, %{body: %ErrorResponse{} = error}} ->
|
||||
{:error, error}
|
||||
|
||||
# Image download path does not attach parse_error; fall back to raw body.
|
||||
{:ok, %{body: body}} ->
|
||||
{:error, body}
|
||||
|
||||
error ->
|
||||
error
|
||||
@@ -74,4 +79,17 @@ defmodule Discogs.API do
|
||||
|
||||
{request, response}
|
||||
end
|
||||
|
||||
defp parse_error({request, %{status: status} = response}) when status not in 200..299 do
|
||||
error = ErrorResponse.from_response(response)
|
||||
|
||||
Logger.error(fn ->
|
||||
url = URI.to_string(request.url)
|
||||
"Failed to fetch data from #{url}, status: #{status}, reason: #{inspect(response.body)}"
|
||||
end)
|
||||
|
||||
Request.halt(request, %{response | body: error})
|
||||
end
|
||||
|
||||
defp parse_error(tuple), do: tuple
|
||||
end
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
defmodule Discogs.API.ErrorResponse do
|
||||
@moduledoc """
|
||||
Structured error response for Discogs API calls.
|
||||
|
||||
Discogs uses HTTP status codes as the error channel. Error bodies are JSON with
|
||||
a single `"message"` field and no application-level error codes.
|
||||
"""
|
||||
|
||||
@behaviour MusicLibrary.ErrorResponse
|
||||
|
||||
alias MusicLibrary.HttpError
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
status: integer() | nil,
|
||||
message: String.t() | nil,
|
||||
kind: HttpError.kind(),
|
||||
body: term()
|
||||
}
|
||||
|
||||
defstruct [:status, :message, :kind, :body]
|
||||
|
||||
@spec from_response(Req.Response.t() | map()) :: t()
|
||||
def from_response(%{status: status, body: body} = _response) do
|
||||
%__MODULE__{
|
||||
status: status,
|
||||
message: extract_message(body),
|
||||
kind: HttpError.default_kind(status),
|
||||
body: body
|
||||
}
|
||||
end
|
||||
|
||||
@impl MusicLibrary.ErrorResponse
|
||||
@spec retryable?(t()) :: boolean()
|
||||
def retryable?(%__MODULE__{kind: kind}) when kind in [:rate_limit, :server_error, :timeout],
|
||||
do: true
|
||||
|
||||
def retryable?(%__MODULE__{}), do: false
|
||||
|
||||
@impl MusicLibrary.ErrorResponse
|
||||
@spec retry_delay_seconds(t()) :: pos_integer()
|
||||
def retry_delay_seconds(%__MODULE__{kind: :rate_limit}), do: 60
|
||||
def retry_delay_seconds(%__MODULE__{kind: :server_error}), do: 30
|
||||
def retry_delay_seconds(%__MODULE__{kind: :timeout}), do: 10
|
||||
def retry_delay_seconds(%__MODULE__{}), do: 30
|
||||
|
||||
defp extract_message(%{"message" => message}) when is_binary(message), do: message
|
||||
defp extract_message(_), do: nil
|
||||
end
|
||||
Reference in New Issue
Block a user