Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling when writing to file during download #390

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/bumblebee/utils/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,7 @@ defmodule Bumblebee.Utils.HTTP do
end

defp download_receive(state, {_, {{_, 200, _}, _headers, body}}) do
case IO.binwrite(state.file, body) do
:ok ->
:ok

{:error, error} ->
{:error, "failed to write to file, reason: #{:file.format_error(error)}"}
end
safe_binwrite(state.file, body)
end

defp download_receive(_state, {_, {{_, status, _}, _headers, _body}}) do
Expand All @@ -90,7 +84,7 @@ defmodule Bumblebee.Utils.HTTP do
end

defp download_receive(state, {_, :stream, body_part}) do
case IO.binwrite(state.file, body_part) do
case safe_binwrite(state.file, body_part) do
:ok ->
part_size = byte_size(body_part)
state = update_in(state.size, &(&1 + part_size))
Expand All @@ -104,7 +98,7 @@ defmodule Bumblebee.Utils.HTTP do

{:error, error} ->
:httpc.cancel_request(state.request_id, :bumblebee)
{:error, "failed to write to file, reason: #{:file.format_error(error)}"}
{:error, error}
end
end

Expand All @@ -122,6 +116,15 @@ defmodule Bumblebee.Utils.HTTP do
end
end

defp safe_binwrite(file, iodata) do
try do
IO.binwrite(file, iodata)
catch
:error, error ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would fail when the file is suddenly closed, not sure when else. We could probably just assert :ok, @josevalim let me know your preference :)

{:error, "failed to write to file, reason: #{inspect(error)}"}
end
end

@doc """
Makes an HTTP request.
Expand Down
Loading