forked from AkkomaGang/akkoma
Merge pull request 'Handle errors from HTTP requests gracefully' (#791) from wp-embeds into develop
Reviewed-on: AkkomaGang/akkoma#791
This commit is contained in:
commit
f8f364d36d
3 changed files with 21 additions and 0 deletions
|
@ -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()}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue