gpt4 book ai didi

ruby-on-rails-5 - Rails5,ActiveJob rescue_from 不工作

转载 作者:行者123 更新时间:2023-12-05 02:19:02 24 4
gpt4 key购买 nike

我正在尝试重现此示例代码:https://apidock.com/rails/ActiveJob/Enqueuing/retry_job但我无法使 rescue_from 在 ActiveJob 中工作

重现步骤

我准备了一个测试项目:

预期行为

在这份工作中:

我希望在执行作业时看到 rescue_from 消息:

irb(main):005:0> TestJobJob.perform_now

实际行为

我看到的不是消息,而是传播的异常:

irb(main):004:0> TestJobJob.perform_now
Performing TestJobJob from Async(default)
Performed TestJobJob from Async(default) in 0.82ms
Exception: This is the error message
from /Users/fguillen/Development/Temp/TestingActiveJobRescueFrom/app/jobs/test_job_job.rb:9:in `perform'
from /Users/fguillen/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activejob-5.0.2/lib/active_job/execution.rb:34:in `block in perform_now'
from /Users/fguillen/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:126:in `call'
from /Users/fguillen/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
from /Users/fguillen/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:455:in `call'
from /Users/fguillen/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:448:in `block (2 levels) in around'
from /Users/fguillen/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:286:in `block (2 levels) in halting'
from /Users/fguillen/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/i18n-0.8.1/lib/i18n.rb:257:in `with_locale'

当我尝试在测试中重现问题时的相同行为:

系统配置

Rails 版本:5.0.2 ruby 版本:2.3.1p112

最佳答案

与实际捕获任何 ExceptionActionController::Base.rescue_from 行为相反:

class MyTestsController < ApplicationController
rescue_from Exception do |exception|
render plain: "Rescuing the exception: '#{exception}'", status: 500
end

def raise_exception
raise Exception.new("This is an Exception")
end

def raise_standard_error
raise StandardError.new("This is an StandardError")
end
end

Example code

ActiveJob::Base.rescue_from 只捕获继承自 StandardError 的类:

class TestJob2Job < ApplicationJob
rescue_from Exception do |exception|
Rails.logger.info "I am on rescue_from: #{exception}"
end

def perform(*args)
raise StandardError.new("This is the error message")
end
end

Example code

您可以在这里看到与维护者的对话:https://github.com/rails/rails/issues/28874#issuecomment-297599254

关于ruby-on-rails-5 - Rails5,ActiveJob rescue_from 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43615039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com