Skip to content

Commit

Permalink
Handle binwrite failures in installer
Browse files Browse the repository at this point in the history
Since Elixir 1.16, the IO.binwrite/2 function no longer returns an
:ok- or :error tuple. Instead, it simply raises an error when it
fails.

To keep compatibility with previous versions of Elixir, this patch
implements binwrite_with_result, which is a delegate of IO.binwrite/2
on versions before 1.16, and a try-catch wrapper for Elixir 1.16 and
above.

With that function in place, the precious implementation is restored,
which prints "Failure!" before the error message in the unlikely event
that the write fails.

Co-authored-by: Tom de Bruijn <tom@tomdebruijn.com>
  • Loading branch information
jeffkreeftmeijer and tombruijn committed Jun 26, 2024
1 parent b65751f commit a4095cf
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/mix/tasks/appsignal.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ defmodule Mix.Tasks.Appsignal.Install do

case File.open(appsignal_config_file_path(), [:write]) do
{:ok, file} ->
IO.binwrite(file, appsignal_config_file_contents(config))
IO.puts("Success!")
case binwrite_with_result(file, appsignal_config_file_contents(config)) do
:ok ->
IO.puts("Success!")

{:error, reason} ->
IO.puts("Failure! #{inspect(reason)}")
exit(:shutdown)
end

File.close(file)

{:error, reason} ->
Expand All @@ -193,6 +200,18 @@ defmodule Mix.Tasks.Appsignal.Install do
end
end

if Version.match?(System.version(), ">= 1.16.0") do
defp binwrite_with_result(path, contents) do
try do
IO.binwrite(path, contents)
catch
{:error, reason} -> {:error, reason}
end
end
else
defdelegate binwrite_with_result(path, contents), to: IO, as: :binwrite
end

# Link the config/appsignal.exs config file to the config/config.exs file.
# If already linked, it's ignored.
defp link_config_file do
Expand Down

0 comments on commit a4095cf

Please sign in to comment.