Move rescue to the HTTP request itself
Some checks failed
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build-amd64 Pipeline was successful
ci/woodpecker/push/build-arm64 Pipeline was successful
ci/woodpecker/push/docs Pipeline was successful
ci/woodpecker/pr/lint Pipeline failed
ci/woodpecker/pr/test unknown status
ci/woodpecker/pr/build-arm64 unknown status
ci/woodpecker/pr/build-amd64 unknown status
ci/woodpecker/pr/docs unknown status

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 @@ def request(method, url, body, headers, options) when is_binary(url) 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

@ -99,11 +99,10 @@ def rich_media_get(url) do
end end
with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options) with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options)
rescue
rescue e ->
e -> Logger.error("Failed to fetch rich media: #{inspect(e)}")
Logger.error("Failed to fetch rich media: #{inspect(e)}") {:error, :fetch_error}
{:error, :fetch_error}
end end
defp check_content_type(headers) do defp check_content_type(headers) do

View file

@ -69,4 +69,14 @@ test "returns successfully result" 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

@ -84,6 +84,8 @@ test "refuses to crawl URLs of private network from posts" do
test "catches errors in fetching" do test "catches errors in fetching" do
Tesla.Mock.mock(fn _ -> raise ArgumentError end) 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
end end