Ruby:两次解救相同的错误类型

Ruby:两次解救相同的错误类型,ruby,exception,exception-handling,Ruby,Exception,Exception Handling,在ruby中是否可以多次挽救相同的错误类型?在使用考拉facebook API库时,我需要: begin # Try with user provided app token fb = Koala::Facebook::API.new(user_access_token) fb.put_connections(user_id, {}) # TODO rescue AuthenticationError # User access token has expired or is f

在ruby中是否可以多次挽救相同的错误类型?在使用考拉facebook API库时,我需要:

begin
  # Try with user provided app token
  fb = Koala::Facebook::API.new(user_access_token)
  fb.put_connections(user_id, {}) # TODO
rescue AuthenticationError
  # User access token has expired or is fake
  fb = Koala::Facebook::API.new(APP_FB_TOKEN)
  fb.put_connections(user_id, {}) # TODO
rescue AuthenticationError
  # User hasn't authed the app on facebook
  puts "Could not authenticate"
  next
rescue => e
  puts "Error when posting to facebook"
  puts e.message
  next
end

如果没有办法挽救同一个错误两次,有没有更好的方法来推理这个问题?

您可能应该将其重构为两个单独的方法-一个处理异常的“安全的”
authenticate
方法和一个“不安全的”
authenticate引发异常的方法<代码>验证
应根据
验证进行定义

下面是一个示例,说明了如何使用重试实现来完成此任务

编辑:重构以使重试独立于
authenticate
方法

def authenticate!
  fb = Koala::Facebook::API.new(user_access_token)
  fb.put_connections(user_id.to_s, )
end

def authenticate
  authenticate!
rescue => e
  warn "Error when posting to facebook: #{e.message}"
end

# @param [Integer] num_retries number of times to try code
def with_retries(num_retries=2)
  yield
rescue AuthenticationError
  # User hasn't authed the app on facebook
  puts "Could not authenticate"
  # Retry up to twice
  (num_retries-=1) < 1 ? raise : retry
end

# Authenticate safely, retrying up to 2 times
with_retries(2) { authenticate }

# Authenticate unsafely, retrying up to 3 times
with_retries(2) { authenticate! }
def认证!
fb=Koala::Facebook::API.new(用户访问令牌)
fb.put_连接(用户id.to_,)
结束
def身份验证
证明…是真实的
救援=>e
警告“发布到facebook时出错:#{e.message}”
结束
#@param[Integer]num\u重试代码的次数
具有重试次数的def(重试次数=2)
产量
救援验证错误
#用户尚未在facebook上验证该应用程序
puts“无法验证”
#最多重试两次
(重试次数-=1)<1?引发:重试
结束
#安全地进行身份验证,最多重试2次
使用_重试(2){authenticate}
#不安全地进行身份验证,最多重试3次
使用_重试(2){authenticate!}

您可能应该将其重构为两个独立的方法—一个是处理异常的“安全的”
authenticate
方法,另一个是“不安全的”
authenticate引发异常的方法<代码>验证
应根据
验证进行定义

下面是一个示例,说明了如何使用重试实现来完成此任务

编辑:重构以使重试独立于
authenticate
方法

def authenticate!
  fb = Koala::Facebook::API.new(user_access_token)
  fb.put_connections(user_id.to_s, )
end

def authenticate
  authenticate!
rescue => e
  warn "Error when posting to facebook: #{e.message}"
end

# @param [Integer] num_retries number of times to try code
def with_retries(num_retries=2)
  yield
rescue AuthenticationError
  # User hasn't authed the app on facebook
  puts "Could not authenticate"
  # Retry up to twice
  (num_retries-=1) < 1 ? raise : retry
end

# Authenticate safely, retrying up to 2 times
with_retries(2) { authenticate }

# Authenticate unsafely, retrying up to 3 times
with_retries(2) { authenticate! }
def认证!
fb=Koala::Facebook::API.new(用户访问令牌)
fb.put_连接(用户id.to_,)
结束
def身份验证
证明…是真实的
救援=>e
警告“发布到facebook时出错:#{e.message}”
结束
#@param[Integer]num\u重试代码的次数
具有重试次数的def(重试次数=2)
产量
救援验证错误
#用户尚未在facebook上验证该应用程序
puts“无法验证”
#最多重试两次
(重试次数-=1)<1?引发:重试
结束
#安全地进行身份验证,最多重试2次
使用_重试(2){authenticate}
#不安全地进行身份验证,最多重试3次
使用_重试(2){authenticate!}

我认为唯一的办法是在
rescue
子句中添加一个新的
try rescue
子句:

begin
  # Try with user provided app token
  fb = Koala::Facebook::API.new(user_access_token)
  fb.put_connections(user_id, {}) # TODO
rescue AuthenticationError
  begin
    # User access token has expired or is fake
    fb = Koala::Facebook::API.new(APP_FB_TOKEN)
    fb.put_connections(user_id, {}) # TODO
  rescue AuthenticationError
    # User hasn't authed the app on facebook
    puts "Could not authenticate"
    next
  end
rescue => e
  puts "Error when posting to facebook"
  puts e.message
  next
end

我认为唯一的办法是在
rescue
子句中添加一个新的
try rescue
子句:

begin
  # Try with user provided app token
  fb = Koala::Facebook::API.new(user_access_token)
  fb.put_connections(user_id, {}) # TODO
rescue AuthenticationError
  begin
    # User access token has expired or is fake
    fb = Koala::Facebook::API.new(APP_FB_TOKEN)
    fb.put_connections(user_id, {}) # TODO
  rescue AuthenticationError
    # User hasn't authed the app on facebook
    puts "Could not authenticate"
    next
  end
rescue => e
  puts "Error when posting to facebook"
  puts e.message
  next
end

关于这一点,我想说的唯一补充是,在第二次重试完成后,可能最终会引发AuhenticateError。如果你最终需要提高它。它将允许其他调用它的代码来处理它,而不仅仅是将它显示在屏幕上。这样,错误就不会最终丢失。将重试代码重构为单独的方法可能更有意义,这样您就可以安全地或不安全地重试。我进行了重构,这样您就可以安全地或不安全地重试。难道不应该
身份验证
调用名为
的私有方法并使用\u retries
传递您想要的块吗?我知道你在做什么,但它应该是内部实现。它可以,但这种方式让你有更多的灵活性来选择是否重试。关于这一点,我想说的唯一补充是,在第二次重试完成后,可能最终会引发AuhenticateError。如果你最终需要提高它。它将允许其他调用它的代码来处理它,而不仅仅是将它显示在屏幕上。这样,错误就不会最终丢失。将重试代码重构为单独的方法可能更有意义,这样您就可以安全地或不安全地重试。我进行了重构,这样您就可以安全地或不安全地重试。难道不应该
身份验证
调用名为
的私有方法并使用\u retries
传递您想要的块吗?我知道你在做什么,但它应该是内部实现。它可以,但这种方式让你更灵活地选择是否重试。