【问题标题】:ActiveJob: Full strack trace despite rescue_from blockActiveJob:尽管有 rescue_from 块,但完整的堆栈跟踪
【发布时间】:2017-10-16 09:17:45
【问题描述】:

我有一个普通的 Rails 应用程序,它只包含一个简单的 Post 模型,带有一个标题和一个内容属性。

我从the official Rails guide中提取了ActiveJob异常示例,如下所示:

class ExceptionTestJob < ApplicationJob
  queue_as :default

  rescue_from(ActiveRecord::RecordNotFound) do |exception|
    logger.error("Could not find given post")
  end

  def perform(post_id)
    post = Post.find(post_id)
  end
end

如果我尝试使用不存在的帖子 ID 运行作业,我会得到完整的堆栈跟踪和错误消息“找不到给定的帖子”。

我不想获取堆栈跟踪。我只想执行rescue_from 块中的代码。如何抑制堆栈跟踪?

编辑

如果我在这样的方法中使用常规的rescue

class ExceptionTestJob < ApplicationJob
  def perform(post_id)
    begin
      Post.find(post_id)
    rescue ActiveRecord::RecordNotFound
      logger.error("Could not find given post")
    end
  end
end

异常将在没有回溯的情况下被捕获(如预期的那样),但作业将成功完成:Performed ExceptionTestJob (Job ID: c21ce397-b19b-4464-9e53-696707b87dde) from Async(default) in 13.84ms

【问题讨论】:

  • 据我了解,您的 rescue_from 块代码正在运行。你接下来想要什么,你能解释一下吗?
  • 我不希望我的日志被整个堆栈跟踪污染。我只想要来自rescue_from 块的简短错误消息。
  • 法比安,除了日志,你想要什么样的响应?
  • 很遗憾这个问题没有得到解决。这仍然是 ActiveJob 的问题
  • 我目前的解决方法是引发一个返回空回溯的异常。

标签: ruby-on-rails ruby exception rails-activejob


【解决方案1】:

我觉得对你有帮助

begin
  raise
rescue => e
  logger.error e.message
  logger.error e.backtrace.join("\n")
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多