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.
  • Loading branch information
jeffkreeftmeijer committed Jun 25, 2024
1 parent eba8a48 commit 494a4d7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/mix/tasks/appsignal.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ defmodule Mix.Tasks.Appsignal.Install do

case File.open(appsignal_config_file_path(), [:write]) do
{:ok, file} ->
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

IO.binwrite(file, appsignal_config_file_contents(config))
IO.puts("Success!")
File.close(file)
Expand All @@ -193,6 +202,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 494a4d7

Please sign in to comment.