Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails错误处理、捕获错误和消息_Ruby On Rails_Ruby_Error Handling_Rescue - Fatal编程技术网

Ruby on rails RubyonRails错误处理、捕获错误和消息

Ruby on rails RubyonRails错误处理、捕获错误和消息,ruby-on-rails,ruby,error-handling,rescue,Ruby On Rails,Ruby,Error Handling,Rescue,我试图找出在RubyonRails中捕获抛出的特定错误和错误消息的最佳方法。我的用例是,我偶尔会遇到一个超时错误,它与一个常规错误一起抛出,我希望将超时错误与同一个常规错误中的其他错误区别对待。我不确定在一般错误中还会抛出哪些其他类型的错误,但我假设存在更多错误。下面我有一些我目前如何处理它的示例代码,但我想可能有一个更好的方法,我还没有找到 tries = 0 begin tries += 1 <code> rescue Foo::Bar => e case e.

我试图找出在RubyonRails中捕获抛出的特定错误和错误消息的最佳方法。我的用例是,我偶尔会遇到一个超时错误,它与一个常规错误一起抛出,我希望将超时错误与同一个常规错误中的其他错误区别对待。我不确定在一般错误中还会抛出哪些其他类型的错误,但我假设存在更多错误。下面我有一些我目前如何处理它的示例代码,但我想可能有一个更好的方法,我还没有找到

tries = 0
begin
  tries += 1
  <code>
rescue Foo::Bar => e
  case e.to_s
  when 'More specific timeout error message'
    retry unless tries >= 5
  else
    # Let me see other error messages
    log.info("Error: #{e.to_s}")
  end
end
看一看。这似乎很适合你的提议。通常,您可以从特定的错误类型中进行救援,但Retreable也可以根据错误消息选择救援

begin
  Retriable.retriable on: { Foo::Bar => /More specific timeout error message/ }, tries: 3 do
    # will retry if an error of type Foo::Bar is raised
    # and its message matches /More specific timeout error message/

    # code here... 
  end
rescue => e # rescue for everything else
  puts e.message # same as e.to_s
end
看一看。这似乎很适合你的提议。通常,您可以从特定的错误类型中进行救援,但Retreable也可以根据错误消息选择救援

begin
  Retriable.retriable on: { Foo::Bar => /More specific timeout error message/ }, tries: 3 do
    # will retry if an error of type Foo::Bar is raised
    # and its message matches /More specific timeout error message/

    # code here... 
  end
rescue => e # rescue for everything else
  puts e.message # same as e.to_s
end

您可以使用multi
rescue
,来处理不同的错误

begin
  # DO SOMETHING
rescue Net::Timeout => e # change it to the error your want to catch, check the log.
  # handle it
rescue SyntaxError => e # just an example 
  # handle it
rescue => e # any error that not catch by above rescue go here.
  # handle it
end
阅读更多:


您可以尝试,它有助于报告生产中的错误。

您可以使用multi
rescue
,来处理不同的错误

begin
  # DO SOMETHING
rescue Net::Timeout => e # change it to the error your want to catch, check the log.
  # handle it
rescue SyntaxError => e # just an example 
  # handle it
rescue => e # any error that not catch by above rescue go here.
  # handle it
end
阅读更多:


您可以尝试,它有助于报告生产中的错误。

您确定使用通用异常类而不是自己的类(如timeout::error)引发超时吗?是的,因此,错误来自一个外部库,该库会不时超时,并返回带有超时消息的外部库常规错误。您确定超时是使用一个通用异常类而不是它自己的类(如timeout::error)引发的吗?是的,因此,错误来自一个外部库,该库不时超时,并返回带有超时消息的外部库常规错误。