Skip to content

Instantly share code, notes, and snippets.

@phildionne
Created September 17, 2018 18:19
Show Gist options
  • Save phildionne/244dffd492fd795eff3a6d2e1ff55f47 to your computer and use it in GitHub Desktop.
Save phildionne/244dffd492fd795eff3a6d2e1ff55f47 to your computer and use it in GitHub Desktop.
# config/application.rb
module MyApp
class Application < Rails::Application
# Handle exceptions with a custom controller
config.exceptions_app = lambda do |env|
ErrorsController.action(:show).call(env)
end
end
end
<%= javascript_tag do %>
document.addEventListener('DOMContentLoaded', function() {
Raven.showReportDialog({
eventId: '<%= @sentry_event_id %>'
})
});
<% end %>
class ErrorsController < ApplicationController
before_action :fetch_exception
layout 'error'
def show
# Get Sentry event for error feedback
@sentry_event_id = Raven.last_event_id
render "errors/#{@rescue_response}", status: @status_code
rescue ActionView::MissingTemplate
render 'errors/internal_server_error', status: 500
end
private
# Resolves an exception to a HTTP error and status
def fetch_exception
@exception = request.env['action_dispatch.exception']
@status_code = ActionDispatch::ExceptionWrapper.status_code_for_exception(@exception.class.name)
@rescue_response = ActionDispatch::ExceptionWrapper.rescue_responses[@exception.class.name]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment