Move rescue to the HTTP request itself

This commit is contained in:
Floatingghost 2024-06-04 14:30:16 +01:00
parent 0f7ae0fa21
commit c9a03af7c1
4 changed files with 21 additions and 6 deletions

View file

@ -75,6 +75,10 @@ defmodule Pleroma.HTTP do
client = Tesla.client([Tesla.Middleware.FollowRedirects, Tesla.Middleware.Telemetry])
request(client, request)
rescue
e ->
Logger.error("Failed to fetch #{url}: #{inspect(e)}")
{:error, :fetch_error}
end
@spec request(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}

View file

@ -99,7 +99,6 @@ defmodule Pleroma.Web.RichMedia.Helpers do
end
with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options)
rescue
e ->
Logger.error("Failed to fetch rich media: #{inspect(e)}")

View file

@ -69,4 +69,14 @@ defmodule Pleroma.HTTPTest do
}
end
end
describe "Catching errors" do
test "rescues when adapter throws an error" do
mock(fn
%{method: :get, url: "http://example.com/hello"} -> raise ArgumentError
end)
assert HTTP.get("http://example.com/hello") == {:error, :fetch_error}
end
end
end

View file

@ -84,6 +84,8 @@ defmodule Pleroma.Web.RichMedia.HelpersTest do
test "catches errors in fetching" do
Tesla.Mock.mock(fn _ -> raise ArgumentError end)
assert {:error, :fetch_error} == Helpers.rich_media_get("wp-json/oembed/1.0/embed?url=http:%252F%252F")
assert {:error, :fetch_error} ==
Helpers.rich_media_get("wp-json/oembed/1.0/embed?url=http:%252F%252F")
end
end