Search code examples
ruby-on-railssentry

Report errors manually from Rails to Sentry


In my ApiController I have:

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

I want that method to also report the error to Sentry. Do you know if by doing the line that does logger.error(...) it automatically logs it in Sentry too?

Or do I have to manually do Raven.capture_exception(exception) or something like that? Will this be done in the background?


Solution

  • Edit 2022

    Raven is deprecated since a few months now and you should use the new Sentry gem.

    To answer the question, which is Report errors manually from Rails to Sentry, you can check the documentation here: https://docs.sentry.io/platforms/ruby/guides/rails/usage/

    TL;DR you can capture exception with

    Sentry.capture_exception(exception)
    

    Or you can capture a message with

    Sentry.capture_message('Something went wrong')