Merge pull request 'Handle errors from HTTP requests gracefully' (#791) from wp-embeds into develop

Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/791
This commit is contained in:
floatingghost 2024-06-07 12:58:58 +00:00
commit f8f364d36d
3 changed files with 21 additions and 0 deletions

View file

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

View file

@ -69,4 +69,14 @@ defmodule Pleroma.HTTPTest do
} }
end end
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 end

View file

@ -81,4 +81,11 @@ defmodule Pleroma.Web.RichMedia.HelpersTest do
assert %{} = Helpers.fetch_data_for_activity(activity4) assert %{} = Helpers.fetch_data_for_activity(activity4)
assert %{} = Helpers.fetch_data_for_activity(activity5) assert %{} = Helpers.fetch_data_for_activity(activity5)
end end
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")
end
end end