Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir/Phoenix handling erlang errors

I'm using System.cmd command to work with a file. However if the files is not found on the system, it raises ArgumentError, specifically Erlang error: :enoent. How can I handle this error with the case function? Here is my code so far:

case System.cmd(generate_executable(settings), ["start"]) do
  {output, 0} ->
    IO.inspect("Start successful")
  {output, error_code} ->
    IO.inspect("Start failed")
end

This cases work for mistakes from OS (whether is starts or not), but not for the erlang errors, resulting in phoenix telling me about :enoent. enter image description here

like image 603
Ilya Avatar asked Aug 29 '26 07:08

Ilya


1 Answers

You'll have to use try/rescue.

try do
  case System.cmd(generate_executable(settings), ["start"]) do
    {output, 0} ->
      IO.inspect("Start successful")
    {output, error_code} ->
      IO.inspect("Start failed")
  end
rescue
  error ->
    IO.inspect(error)
end

When the executable does not exist, you should see %ErlangError{original: :enoent} printed by the IO.inspect in rescue.

like image 121
Dogbert Avatar answered Sep 01 '26 05:09

Dogbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!