我在这里丢失了一些东西,但是由于某种原因,我的runserver.py
和begin
Ruby代码无法捕获此错误:
rescue
这是我的代码:
#<ActiveResource::ResourceInvalid: Failed. Response code = 422. Response message = Unprocessable Entity.>
它永远不会捕获它。在我的begin
ShopifyAPI::CarrierService.create(with some arguments)
rescue StandardError => e
pp e
end
部分中,我尝试了以上操作,但也进行了以下操作:
rescue
一切都没有运气。我在哪里迷路了?
谢谢
编辑: 这是一个完整的错误,它不再是真正的信息了,但是在这里:
rescue Exception => e
rescue ActiveResource::Errors => e
就是这样!
答案 0 :(得分:2)
因为它没有引发异常,所以如果您想在响应为假时引发异常,则可能必须使用create with bang
begin
ShopifyAPI::CarrierService.create!(with some arguments)
rescue StandardError => e
pp e
end
答案 1 :(得分:1)
根据ActiveResource代码(lib / active_resource / base.rb):
# <tt>404</tt> is just one of the HTTP error response codes that Active Resource will handle with its own exception. The
# following HTTP response codes will also result in these exceptions:
#
# * 200..399 - Valid response. No exceptions, other than these redirects:
# * 301, 302, 303, 307 - ActiveResource::Redirection
# * 400 - ActiveResource::BadRequest
# * 401 - ActiveResource::UnauthorizedAccess
# * 403 - ActiveResource::ForbiddenAccess
# * 404 - ActiveResource::ResourceNotFound
...
# * 422 - ActiveResource::ResourceInvalid (rescued by save as validation errors)
因此,它表明通过保存验证来挽救422,这是在触发.create时发生的,并冒泡为验证错误。
查看lib / active_resource / validations.rb,可以看到ResourceInvalid异常被吞噬了:
# Validate a resource and save (POST) it to the remote web service.
# If any local validations fail - the save (POST) will not be attempted.
def save_with_validation(options={})
perform_validation = options[:validate] != false
# clear the remote validations so they don't interfere with the local
# ones. Otherwise we get an endless loop and can never change the
# fields so as to make the resource valid.
@remote_errors = nil
if perform_validation && valid? || !perform_validation
save_without_validation
true
else
false
end
rescue ResourceInvalid => error
# cache the remote errors because every call to <tt>valid?</tt> clears
# all errors. We must keep a copy to add these back after local
# validations.
@remote_errors = error
load_remote_errors(@remote_errors, true)
false
end
因此,我想知道是否正在记录是否发生了异常,但实际上并没有引发异常,因为它将异常返回了false。它确实在注释中说了“本地验证”,但是设置了remote_errors,因此尚不清楚该代码路径在何处执行。