Ruby 仅救援特定OAuth2错误
我使用OAuth2以以下格式发出请求:Ruby 仅救援特定OAuth2错误,ruby,oauth,Ruby,Oauth,我使用OAuth2以以下格式发出请求: @access_token = get_access_token begin @access_token.get(...).body rescue OAuth::Error => e # handle errors end 这是可行的,但它可以挽救所有OAuth2错误,我只想挽救特定的错误 例如,如果我只想挽救未经授权的错误。是否存在类似于OAuth::Error::Unauthorized的代码,或者可能只是使用响应代码OAuth::E
@access_token = get_access_token
begin
@access_token.get(...).body
rescue OAuth::Error => e
# handle errors
end
这是可行的,但它可以挽救所有OAuth2错误,我只想挽救特定的错误
例如,如果我只想挽救未经授权的错误。是否存在类似于OAuth::Error::Unauthorized
的代码,或者可能只是使用响应代码OAuth::Error::401
有没有办法限制我从哪个
OAuth
错误中解救出来?我不知道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
除了在e.class
上使用if
,您还可以通过e.message
和e.backtrace
获得有关错误的更多信息。可能类似于如果e.message.include?(“一些字符串”)
如果已确定存在错误的子类,则可以仅用该子类替换
rescue Oauth::error
我不知道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
除了在e.class
上使用if
,您还可以通过e.message
和e.backtrace
获得有关错误的更多信息。可能类似于如果e.message.include?(“一些字符串”)
如果已确定存在错误的子类,则可以仅用该子类替换rescue Oauth::error