救援,但仍然记录哨兵/新遗物通知/记录错误

时间:2020-10-15 18:03:22

标签: ruby ruby-on-rails-6 newrelic sentry

想要记录出现错误时可以与Sentry / New Relic等配合使用的错误,但我可以挽救它。目前,我使用Sentry,它将通过电子邮件将所有出现的错误发送给我,以便我进行修复。但是,当我挽救错误时,不会引发任何错误,因此不知道有任何错误。

class Contact
  include ActiveModel::Model
  attr_accessor :name, :email, :message

  validates :name, :phone, :message, presence: true
  validates :email, format: {with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i}

  def deliver
    if valid?
      begin
        return ContactUsMailer.contact(self).deliver
      rescue StandardError
        errors.add(:base, I18n.t("messages.unprocessed"))
        # Want to log an error that I can use with a Sentry/New Relic, etc.
        # Currently I use Sentry and it emails me all raised errors to I can fix them.
        false
      end
    end

    false
  end
end

2 个答案:

答案 0 :(得分:1)

假设您拥有sentry-raven gem设置,也可以调用

Raven.capture_exception(e)

您的示例应该是

class Contact
  include ActiveModel::Model
  attr_accessor :name, :email, :message

  validates :name, :phone, :message, presence: true
  validates :email, format: {with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i}

  def deliver
    if valid?
      begin
        return ContactUsMailer.contact(self).deliver
      rescue StandardError => e
        Raven.capture_exception(e)
        errors.add(:base, I18n.t("messages.unprocessed"))
        # Want to log an error that I can use with a Sentry/New Relic, etc.
        # Currently I use Sentry and it emails me all raised errors to I can fix them.
        false
      end
    end

    false
  end
end

以确保仍将问题报告给哨兵。

如果您的目标是仅报告任何文本,答案将是

Raven.capture_message("your text")

另请参阅:https://docs.sentry.io/platforms/ruby/usage/#reporting-messages

答案 1 :(得分:1)

您可以manually log将错误发送给Sentry:

begin
  return ContactUsMailer.contact(self).deliver
rescue StandardError => exception
  errors.add(:base, I18n.t("messages.unprocessed"))
  Raven.capture_exception(exception)
  false
end