仅抢救特定的OAuth2错误

时间:2017-01-11 19:55:46

标签: ruby oauth

我正在使用OAuth2以下列格式发出请求:

OAuth::Error::401

这可行,但它可以解救 所有 OAuth2错误,我只想拯救特定的错误。

例如,如果我只想拯救未经授权的错误。是否有类似OAuth或仅使用回复代码yyy的响应代码。

有没有办法限制我拯救的db.testcol.update({_id: 111, yyy: {$exists: true}}, {$set: {yyy: 'ccc'}}) 错误?

1 个答案:

答案 0 :(得分:2)

我不明确知道OAuth :: Error是否具有子类。

但这是拯救特定错误的普遍适用方式。

begin
  # raise error here
rescue OAuth::Error => e
  puts e.class # => OAuth::Error or some subclass
  if e.class == OAuth::Error::SomeSubclass
    # continue with rescue
  else
    raise e # the equivalent of "super" from a rescue block
            # i.e. act like the rescue didn't happen
  end
end

除了在if上使用e.class之外,您还可以使用e.messagee.backtrace获取有关错误的更多信息。也许像if e.message.include?("some string")

如果您确定存在错误的子类,则可以仅使用子类替换rescue Oauth::Error