Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions lib/raven/integrations/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ class Rails < ::Rails::Railtie
end

config.after_initialize do
if Raven.configuration.catch_debugged_exceptions
require 'raven/integrations/rails/middleware/debug_exceptions_catcher'
if defined?(::ActionDispatch::DebugExceptions)
exceptions_class = ::ActionDispatch::DebugExceptions
elsif defined?(::ActionDispatch::ShowExceptions)
exceptions_class = ::ActionDispatch::ShowExceptions
end
unless exceptions_class.nil?
if RUBY_VERSION.to_f < 2.0
exceptions_class.send(:include, Raven::Rails::Middleware::OldDebugExceptionsCatcher)
else
exceptions_class.send(:prepend, Raven::Rails::Middleware::DebugExceptionsCatcher)
end
require 'raven/integrations/rails/middleware/debug_exceptions_catcher'
if defined?(::ActionDispatch::DebugExceptions)
exceptions_class = ::ActionDispatch::DebugExceptions
elsif defined?(::ActionDispatch::ShowExceptions)
exceptions_class = ::ActionDispatch::ShowExceptions
end
unless exceptions_class.nil?
if RUBY_VERSION.to_f < 2.0
exceptions_class.send(:include, Raven::Rails::Middleware::OldDebugExceptionsCatcher)
else
exceptions_class.send(:prepend, Raven::Rails::Middleware::DebugExceptionsCatcher)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module DebugExceptionsCatcher
def render_exception(env_or_request, exception)
begin
env = env_or_request.respond_to?(:env) ? env_or_request.env : env_or_request
Raven::Rack.capture_exception(exception, env)
Raven::Rack.capture_exception(exception, env) if Raven.configuration.catch_debugged_exceptions
rescue # rubocop:disable Lint/HandleExceptions
end
super
Expand All @@ -20,7 +20,7 @@ def self.included(base)
def render_exception_with_raven(env_or_request, exception)
begin
env = env_or_request.respond_to?(:env) ? env_or_request.env : env_or_request
Raven::Rack.capture_exception(exception, env)
Raven::Rack.capture_exception(exception, env) if Raven.configuration.catch_debugged_exceptions
rescue # rubocop:disable Lint/HandleExceptions
end
render_exception_without_raven(env_or_request, exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ def render_exception(_, exception)

let(:env) { {} }

context "using include" do
before do
middleware.send(:include, Raven::Rails::Middleware::OldDebugExceptionsCatcher)
end

shared_examples "the debug exceptions middleware" do
it "shows the exception" do
expect(middleware.new(app).call(env)).to eq([500, "app error", {}])
end
Expand All @@ -46,28 +42,41 @@ def render_exception(_, exception)
expect(middleware.new(app).call(env)).to eq([500, "app error", {}])
end
end

context "when catch_debugged_exceptions is disabled" do
before do
Raven.configure do |config|
config.catch_debugged_exceptions = false
end
end

after do
Raven.configure do |config|
config.catch_debugged_exceptions = true
end
end

it "doesn't capture the exception" do
expect(Raven::Rack).not_to receive(:capture_exception)
middleware.new(app).call(env)
end
end
end

context "using prepend" do
context "using include" do
before do
skip "prepend not available" unless middleware.respond_to?(:prepend, true)
middleware.send(:prepend, Raven::Rails::Middleware::DebugExceptionsCatcher)
middleware.send(:include, Raven::Rails::Middleware::OldDebugExceptionsCatcher)
end

it "shows the exception" do
expect(middleware.new(app).call(env)).to eq([500, "app error", {}])
end
it_behaves_like "the debug exceptions middleware"
end

it "captures the exception" do
expect(Raven::Rack).to receive(:capture_exception)
middleware.new(app).call(env)
context "using prepend" do
before do
skip "prepend not available" unless middleware.respond_to?(:prepend, true)
middleware.send(:prepend, Raven::Rails::Middleware::DebugExceptionsCatcher)
end

context "when an error is raised" do
it "shows the original exception" do
allow(Raven::Rack).to receive(:capture_exception).and_raise("raven error")
expect(middleware.new(app).call(env)).to eq([500, "app error", {}])
end
end
it_behaves_like "the debug exceptions middleware"
end
end
20 changes: 20 additions & 0 deletions spec/raven/integrations/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@
it "doesn't clobber a manually configured release" do
expect(Raven.configuration.release).to eq('beta')
end

context "when catch_debugged_exceptions is disabled" do
before do
Raven.configure do |config|
config.catch_debugged_exceptions = false
end
end

after do
Raven.configure do |config|
config.catch_debugged_exceptions = true
end
end

it "doesn't capture exceptions automatically" do
get "/exception"
expect(response.status).to eq(500)
expect(Raven.client.transport.events.size).to eq(0)
end
end
end