error-handling - Elixir/Phoenix 处理 erlang 错误

标签 error-handling elixir phoenix-framework

我正在使用 System.cmd 命令来处理文件。但是,如果在系统上找不到文件,则会引发 ArgumentError ,特别是 Erlang 错误::enoent。 如何使用 case 函数处理此错误?到目前为止,这是我的代码:

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

这种情况适用于操作系统错误(无论是否启动),但不适用于 erlang 错误,导致 phoenix 告诉我有关 :enoent 的信息。 enter image description here

最佳答案

你必须使用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

当可执行文件不存在时,您应该看到%ErlangError{original: :enoent}rescue 中的 IO.inspect 打印出来.

关于error-handling - Elixir/Phoenix 处理 erlang 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46011877/

相关文章:

elixir - 在 Elixir 中缓存昂贵的计算

php - 如何判断文件是否可从脚本写入?

android - 是否有SDK可以记录生产应用中的异常,事件和错误?

Elixir:如何获取列表中的最后 n 个项目?

json - Poison.Encoder 如何重命名渲染 JSON 中的属性?

elixir - 如何使用phoenix对两种参数进行模式匹配

elixir - Phoenix 开始抛出 (UndefinedFunctionError) 函数 :crypto. rand_bytes/1 is undefined or private

sql-server - 为什么我的SQL代码抛出错误

vba - 私有(private)子上的excel类型不匹配错误

erlang - Elixir/Erlang 透析器 : Why behaviour callback's param type should be subtype instead of supertype?