http: safely catch erlang exits and elixir errors from hackney (ref #672)
authorWilliam Pitcock <nenolod@dereferenced.org>
Thu, 7 Mar 2019 23:18:59 +0000 (23:18 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Fri, 8 Mar 2019 22:56:16 +0000 (22:56 +0000)
lib/pleroma/http/http.ex

index 26214ef3feca94c4af8e12b2b07c832b0a3c0c7d..c6d86b3d3768bc0ce2113b864a264bc977b88d89 100644 (file)
@@ -27,22 +27,29 @@ defmodule Pleroma.HTTP do
 
   """
   def request(method, url, body \\ "", headers \\ [], options \\ []) do
-    options =
-      process_request_options(options)
-      |> process_sni_options(url)
-      |> process_adapter_options()
-
-    params = Keyword.get(options, :params, [])
-
-    %{}
-    |> Builder.method(method)
-    |> Builder.headers(headers)
-    |> Builder.opts(options)
-    |> Builder.url(url)
-    |> Builder.add_param(:body, :body, body)
-    |> Builder.add_param(:query, :query, params)
-    |> Enum.into([])
-    |> (&Tesla.request(Connection.new(), &1)).()
+    try do
+      options =
+        process_request_options(options)
+        |> process_sni_options(url)
+
+      params = Keyword.get(options, :params, [])
+
+      %{}
+      |> Builder.method(method)
+      |> Builder.headers(headers)
+      |> Builder.opts(options)
+      |> Builder.url(url)
+      |> Builder.add_param(:body, :body, body)
+      |> Builder.add_param(:query, :query, params)
+      |> Enum.into([])
+      |> (&Tesla.request(Connection.new(), &1)).()
+    rescue
+      e ->
+        {:error, e}
+    catch
+      :exit, e ->
+        {:error, e}
+    end
   end
 
   defp process_sni_options(options, nil), do: options
@@ -57,12 +64,6 @@ defmodule Pleroma.HTTP do
     end
   end
 
-  def process_adapter_options(options) do
-    adapter_options = Pleroma.Config.get([:http, :adapter], [])
-
-    options ++ [adapter: adapter_options]
-  end
-
   def process_request_options(options) do
     config = Application.get_env(:pleroma, :http, [])
     proxy = Keyword.get(config, :proxy_url, nil)