From 30e13a878505d16a98f26cef0fe8a4a68f3675e9 Mon Sep 17 00:00:00 2001 From: Floatingghost Date: Tue, 4 Jun 2024 14:21:40 +0100 Subject: [PATCH 1/5] Don't error on rich media fail --- lib/pleroma/web/rich_media/helpers.ex | 5 +++++ test/pleroma/web/rich_media/helpers_test.exs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex index 061c1a795..6b2f7ee02 100644 --- a/lib/pleroma/web/rich_media/helpers.ex +++ b/lib/pleroma/web/rich_media/helpers.ex @@ -97,6 +97,11 @@ def rich_media_get(url) do end with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options) + + rescue + e -> + Logger.error("Failed to fetch rich media: #{inspect(e)}") + {:error, :fetch_error} end defp check_content_type(headers) do diff --git a/test/pleroma/web/rich_media/helpers_test.exs b/test/pleroma/web/rich_media/helpers_test.exs index c6c3ffd6c..3fa7eecef 100644 --- a/test/pleroma/web/rich_media/helpers_test.exs +++ b/test/pleroma/web/rich_media/helpers_test.exs @@ -81,4 +81,9 @@ 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 From 0f7ae0fa21ff95f79fbbae83c657cfe0e1517f53 Mon Sep 17 00:00:00 2001 From: Floatingghost Date: Tue, 4 Jun 2024 14:26:33 +0100 Subject: [PATCH 2/5] am i baka --- lib/pleroma/web/rich_media/helpers.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex index 6b2f7ee02..2045a6a11 100644 --- a/lib/pleroma/web/rich_media/helpers.ex +++ b/lib/pleroma/web/rich_media/helpers.ex @@ -9,6 +9,8 @@ defmodule Pleroma.Web.RichMedia.Helpers do alias Pleroma.Object alias Pleroma.Web.RichMedia.Parser + require Logger + @options [ max_body: 2_000_000, receive_timeout: 2_000 From c9a03af7c1a4ff0b8398d65f88a2cb5fb8549566 Mon Sep 17 00:00:00 2001 From: Floatingghost Date: Tue, 4 Jun 2024 14:30:16 +0100 Subject: [PATCH 3/5] Move rescue to the HTTP request itself --- lib/pleroma/http.ex | 4 ++++ lib/pleroma/web/rich_media/helpers.ex | 9 ++++----- test/pleroma/http_test.exs | 10 ++++++++++ test/pleroma/web/rich_media/helpers_test.exs | 4 +++- 4 files changed, 21 insertions(+), 6 deletions(-) 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/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex index 2045a6a11..790ec4c1b 100644 --- a/lib/pleroma/web/rich_media/helpers.ex +++ b/lib/pleroma/web/rich_media/helpers.ex @@ -99,11 +99,10 @@ def rich_media_get(url) do end with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options) - - rescue - e -> - Logger.error("Failed to fetch rich media: #{inspect(e)}") - {:error, :fetch_error} + rescue + e -> + Logger.error("Failed to fetch rich media: #{inspect(e)}") + {:error, :fetch_error} end defp check_content_type(headers) do 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 3fa7eecef..4d8a04788 100644 --- a/test/pleroma/web/rich_media/helpers_test.exs +++ b/test/pleroma/web/rich_media/helpers_test.exs @@ -84,6 +84,8 @@ test "refuses to crawl URLs of private network from posts" 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 From 38d09cb0ce48f86cf8af0a99f6e8ae2d8fa58abf Mon Sep 17 00:00:00 2001 From: Floatingghost Date: Tue, 4 Jun 2024 14:34:18 +0100 Subject: [PATCH 4/5] remove now-pointless clause --- lib/pleroma/web/rich_media/helpers.ex | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex index 790ec4c1b..cb381d86b 100644 --- a/lib/pleroma/web/rich_media/helpers.ex +++ b/lib/pleroma/web/rich_media/helpers.ex @@ -99,10 +99,6 @@ def rich_media_get(url) do end with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options) - rescue - e -> - Logger.error("Failed to fetch rich media: #{inspect(e)}") - {:error, :fetch_error} end defp check_content_type(headers) do From 0f65dd3ebecaa211cabc24699a3f12e0a44e09cb Mon Sep 17 00:00:00 2001 From: Floatingghost Date: Tue, 4 Jun 2024 14:34:59 +0100 Subject: [PATCH 5/5] remove pointless logger --- lib/pleroma/web/rich_media/helpers.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex index cb381d86b..061c1a795 100644 --- a/lib/pleroma/web/rich_media/helpers.ex +++ b/lib/pleroma/web/rich_media/helpers.ex @@ -9,8 +9,6 @@ defmodule Pleroma.Web.RichMedia.Helpers do alias Pleroma.Object alias Pleroma.Web.RichMedia.Parser - require Logger - @options [ max_body: 2_000_000, receive_timeout: 2_000