Sentry-ruby: Add support for User Feedback

Created on 21 Apr 2016  路  11Comments  路  Source: getsentry/sentry-ruby

GH-493 is intended to make it possible to do this, but we should determine, at the very least, what the best practice is for Rails, and document that.

Here's Django:

https://docs.getsentry.com/hosted/clients/python/integrations/django/#user-feedback

(Source: https://github.com/getsentry/raven-python/blob/master/docs/integrations/django.rst#user-feedback)

enhancement sentry spec

Most helpful comment

If this would be useful for anyone, here's full example how I managed to make this work, thanks all for sharing their information.

app/controllers/errors_controller.rb

class ErrorsController < ApplicationController
  def internal_server_error
    @sentry_event_id = Raven.last_event_id
    @sentry_dsn = unless ENV['SENTRY_DSN'].nil? then ENV['SENTRY_DSN'].gsub(/^https:\/\/([^:]+):[^@]+(.*)/, 'https://\1\2') end
     render(
      status: 500,
    )
  end
end

In SENTRY_DSN is something like https://public_key:[email protected]/project_id
So you need to remove the private key, that's what the substitution does.

app/views/application/internal_server_error.html.erb

<% if @sentry_event_id %>
  <!-- Sentry JS SDK 2.1.+ required -->
  <script src="https://cdn.ravenjs.com/3.15.0/raven.min.js"></script>
  <script>
    Raven.showReportDialog({
      eventId: '<%= @sentry_event_id %>',
      dsn: '<%= @sentry_dsn %>'
    })
  </script>
<% end %>
 Something went wrong.

config/application.rb

    config.exceptions_app = self.routes

config/routes.rb

  get '500' => 'errors#internal_server_error'

All 11 comments

In the Rails router you can configure an exception handler. It would be nice if there was plug-and-play integration with that so it's easy to replace the default Rails error page:

500

With the nice user feedback modal.

That would be super awesome. We have the basic support (get last event ID now), so I could imagine this being fairly easy to get up and running.

@seanlinsley What are you referring to? rescue_from or something else?

@nateberkopec I'm sure there's a more portable way to do it, but our application does this:

# config/application.rb
config.exceptions_app = self.routes

# config/routes.rb
get '404' => 'errors#error_404'
get '422' => 'errors#error_422'
get '500' => 'errors#error_500'

# app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
  def error_404
    respond_to do |format|
      format.html { render status: 404 }
      format.json { render json: {error: '404 Not Found'}, status: 404 }
      format.any  { render text:         '404 Not Found',  status: 404 }
    end
  end

  def error_422
    respond_to do |format|
      format.html { render status: 422 }
      format.json { render json: {error: '422 Unprocessable Entity'}, status: 422 }
      format.any  { render text:         '422 Unprocessable Entity',  status: 422 }
    end
  end

  def error_500
    respond_to do |format|
      format.html { render status: 500 }
      format.json { render json: {error: '500 Internal Server Error'}, status: 500 }
      format.any  { render text:         '500 Internal Server Error',  status: 500 }
    end
  end
end

Then we have branded templates for each action.

Got it! I was looking for config.exceptions_app - exception reporting in Rails is kind of half-baked in that there's many places and ways to define how exceptions should be handled, and none of them cover _all_ types of exceptions.

So I might be dumb, but the example we committed in docs I can't seem to get to work. I had to do this:

class ErrorsController < ApplicationController
  def internal_server_error
    @sentry_event_id = Raven.last_event_id
    render(:status => 500)
  end
end

Also, we suggest disabling the exceptions_app functionality so I took this approach:

    # https://github.com/getsentry/raven-ruby/issues/494
    config.exceptions_app = self.routes

    # With this enabled 'exceptions_app' isnt executed, so instead we
    # set ``config.consider_all_requests_local = false`` in development.
    # config.action_dispatch.show_exceptions = false

Anyone successfully have User Feedback working? I've been following the docs, and trying to translate the Django instructions. I didn't realize that this isn't implementable in Rails. (?) https://docs.sentry.io/learn/user-feedback/

@dogweather did you try out the example from https://github.com/getsentry/raven-ruby/issues/494#comment-227929105

?

IIRC I once got that working, with some modifications maybe

Thanks @klyonrad ! Yep, I just got it deployed and working, pretty much by combining what everyone's contributed here. :-) I'll write up a blog post. It'll be nice to have it one place.

@dogweather did you ever write that blog post? I'm interested in doing this too, but not sure how to proceed

If this would be useful for anyone, here's full example how I managed to make this work, thanks all for sharing their information.

app/controllers/errors_controller.rb

class ErrorsController < ApplicationController
  def internal_server_error
    @sentry_event_id = Raven.last_event_id
    @sentry_dsn = unless ENV['SENTRY_DSN'].nil? then ENV['SENTRY_DSN'].gsub(/^https:\/\/([^:]+):[^@]+(.*)/, 'https://\1\2') end
     render(
      status: 500,
    )
  end
end

In SENTRY_DSN is something like https://public_key:[email protected]/project_id
So you need to remove the private key, that's what the substitution does.

app/views/application/internal_server_error.html.erb

<% if @sentry_event_id %>
  <!-- Sentry JS SDK 2.1.+ required -->
  <script src="https://cdn.ravenjs.com/3.15.0/raven.min.js"></script>
  <script>
    Raven.showReportDialog({
      eventId: '<%= @sentry_event_id %>',
      dsn: '<%= @sentry_dsn %>'
    })
  </script>
<% end %>
 Something went wrong.

config/application.rb

    config.exceptions_app = self.routes

config/routes.rb

  get '500' => 'errors#internal_server_error'
Was this page helpful?
0 / 5 - 0 ratings