Skip to content

Commit

Permalink
refactor using with, bubble error tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
emoragaf committed Jun 28, 2022
1 parent 99aed7c commit 51a7b50
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions lib/waffle/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ defmodule Waffle.File do
{:ok, local_path} ->
%Waffle.File{path: local_path, file_name: filename, is_tempfile?: true}

{:error, _reason} = err ->
err

:error ->
{:error, :invalid_file_path}
end
Expand All @@ -41,8 +44,14 @@ defmodule Waffle.File do
uri = URI.parse(remote_path)

case save_file(uri, filename, definition) do
{:ok, local_path} -> %Waffle.File{path: local_path, file_name: filename, is_tempfile?: true}
:error -> {:error, :invalid_file_path}
{:ok, local_path} ->
%Waffle.File{path: local_path, file_name: filename, is_tempfile?: true}

{:error, _reason} = err ->
err

:error ->
{:error, :invalid_file_path}
end
end

Expand Down Expand Up @@ -140,7 +149,7 @@ defmodule Waffle.File do
case save_temp_file(local_path, uri, definition) do
{:ok, filename} -> {:ok, local_path, filename}
:ok -> {:ok, local_path}
_ -> :error
err -> err
end
end

Expand All @@ -157,8 +166,8 @@ defmodule Waffle.File do
{:ok, body} ->
File.write(local_path, body)

{:error, error} ->
{:error, error}
{:error, _reason} = err ->
err
end
end

Expand All @@ -182,21 +191,29 @@ defmodule Waffle.File do
end

defp request(remote_path, headers, options, tries \\ 0) do
case :hackney.get(URI.to_string(remote_path), headers, "", options) do
{:ok, 200, response_headers, client_ref} ->
body(client_ref, response_headers, tries)
with {:ok, 200, response_headers, client_ref} <-
:hackney.get(URI.to_string(remote_path), headers, "", options),
res when elem(res, 0) == :ok <- body(client_ref, response_headers) do
res
else
{:error, %{reason: :timeout}} ->
case retry(tries, options) do
{:ok, :retry} -> request(remote_path, headers, options, tries + 1)
{:error, :out_of_tries} -> {:error, :timeout}
end

_ ->
{:error, :timeout} ->
case retry(tries, options) do
{:ok, :retry} -> request(remote_path, headers, options, tries + 1)
{:error, :out_of_tries} -> {:error, :recv_timeout}
end

_err ->
{:error, :waffle_hackney_error}
end
end

defp body(client_ref, response_headers, tries) do
defp body(client_ref, response_headers) do
case :hackney.body(client_ref) do
{:ok, body} ->
response_headers = :hackney_headers.new(response_headers)
Expand All @@ -207,11 +224,9 @@ defmodule Waffle.File do
else
{:ok, body, filename}
end
{:error, :timeout} ->
case retry(tries, options) do
{:ok, :retry} -> request(remote_path, headers, options, tries + 1)
{:error, :out_of_tries} -> {:error, :recv_timeout}
err -> err

err ->
err
end
end

Expand Down

0 comments on commit 51a7b50

Please sign in to comment.