Junaid Farooq
Junaid Farooq

Reputation: 2598

sending HTTPoison request! in a try and catch manner elixir

I am trying to send an POST request using Elixir Dropbox package, its very simple though

case ElixirDropbox.Files.upload(client, upload_image_path, image_path) do
  {{:status_code, _}, {:error, error}} -> Logger.debug "Error while uploading. Error: #{inspect error}"
  _ -> :noop
end

the issue is: The library I am using is doing request! instead of request and it's not giving any response in case of failure with a status code it raises an exception of timeout as

** (HTTPoison.Error) :timeout
    (httpoison) lib/httpoison.ex:66: HTTPoison.request!/5
    (elixir_dropbox) lib/elixir_dropbox.ex:36: ElixirDropbox.post_request/4
    (evercam_media) lib/evercam_media/snapshot_extractor/extractor.ex:132: 

How can we handle such situation? in try, catch, rescue?,

what I simply want to do is: In case of any exception or failure, from dropbox API, I want to retry upload again, with , lets say 5 tries.

  defp upload_image("true", image_path, upload_image_path) do
    client = ElixirDropbox.Client.new(System.get_env["DROP_BOX_TOKEN"])
    case ElixirDropbox.Files.upload(client, upload_image_path, image_path) do
      {{:status_code, _}, {:error, error}} -> Logger.debug "Error while uploading. Error: #{inspect error}"
      _ -> :noop
    end
  end

Upvotes: 0

Views: 718

Answers (1)

Dogbert
Dogbert

Reputation: 222148

You can use try/rescue to catch this error:

try do
  HTTPoison.request!(...)
rescue
  HTTPoison.Error ->
    # the request raised an error
end

Upvotes: 1

Related Questions