Reputation: 1150
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.
Upvotes: 3
Views: 596
Reputation: 222040
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
.
Upvotes: 6