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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With