diff --git a/lib/pleroma/http.ex b/lib/pleroma/http.ex index 9346ffa16..28d6e3a36 100644 --- a/lib/pleroma/http.ex +++ b/lib/pleroma/http.ex @@ -75,6 +75,10 @@ def request(method, url, body, headers, options) when is_binary(url) 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()} diff --git a/test/pleroma/http_test.exs b/test/pleroma/http_test.exs index e8a2538d3..8619196ad 100644 --- a/test/pleroma/http_test.exs +++ b/test/pleroma/http_test.exs @@ -69,4 +69,14 @@ test "returns successfully result" 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 diff --git a/test/pleroma/web/rich_media/helpers_test.exs b/test/pleroma/web/rich_media/helpers_test.exs index c6c3ffd6c..4d8a04788 100644 --- a/test/pleroma/web/rich_media/helpers_test.exs +++ b/test/pleroma/web/rich_media/helpers_test.exs @@ -81,4 +81,11 @@ test "refuses to crawl URLs of private network from posts" do assert %{} = Helpers.fetch_data_for_activity(activity4) assert %{} = Helpers.fetch_data_for_activity(activity5) 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