从 Rails 向哨兵手动报告错误

Report errors manually from Rails to Sentry

在我的 ApiController 中我有:

rescue_from Exception, with: :render_exception_error

  def render_exception_error(exception)
    raise exception if Rails.env.test?

    logger.error(exception)
    render json: { error: 'An error occurred' }, status: 500 unless performed?
  end

我希望该方法也向哨兵报告错误。 您知道是否通过执行 logger.error(...) 行它也会自动将其记录在 Sentry 中吗?

或者我必须手动执行 Raven.capture_exception(exception) 或类似的操作吗?这会在后台完成吗?

编辑 2022

Raven 几个月前就被弃用了,您应该使用新的 Sentry gem。

要回答从Rails手动报告错误到哨兵的问题,您可以在此处查看文档:https://docs.sentry.io/platforms/ruby/guides/rails/usage/

TL;DR 你可以用

捕获异常
Sentry.capture_exception(exception)

或者您可以使用

捕获消息
Sentry.capture_message('Something went wrong')