Merge pull request 'Handle errors from HTTP requests gracefully' (#791) from wp-embeds into develop
Some checks are pending
ci/woodpecker/push/build-amd64 Pipeline is pending
ci/woodpecker/push/build-arm64 Pipeline is pending
ci/woodpecker/push/docs Pipeline is pending
ci/woodpecker/push/lint Pipeline is pending
ci/woodpecker/push/test Pipeline is pending

Reviewed-on: #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 @@ 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()}

View file

@ -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

View file

@ -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