3
votes

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

1

1 Answers

6
votes

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.