From a530de77e65379b987f2ff68c96e21969a059767 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Thu, 23 Jul 2020 10:42:00 +0100 Subject: [PATCH] Supress frozen middleware errors in Railtie This should never happen in normal operation, but is possible when running RSpec tests See https://github.com/thoughtbot/factory_bot_rails/issues/303#issuecomment-434560625 and https://github.com/bugsnag/bugsnag-ruby/issues/534 Essentially this happens: 1. RSpec boots Rails 2. Rails boot process errors 3. Rails freezes middleware 4. RSpec moves on to the next test file 5. Bugsnag tries to add middleware but fails because it's frozen 6. The stacktrace blames Bugsnag for this test failure 7. goto 4 Supressing this error doesn't solve the problem as it's likely another frozen error will happen in some other library/component, but we want to avoid people thinking this is caused by Bugsnag. The stacktrace does point out the actual problem, but only in the very first error, which can get buried under the frozen errors that happen for each test file --- lib/bugsnag/integrations/railtie.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/bugsnag/integrations/railtie.rb b/lib/bugsnag/integrations/railtie.rb index 763ab7f16..015c66620 100644 --- a/lib/bugsnag/integrations/railtie.rb +++ b/lib/bugsnag/integrations/railtie.rb @@ -63,9 +63,19 @@ class Railtie < ::Rails::Railtie initializer "bugsnag.use_rack_middleware" do |app| begin - app.config.middleware.insert_after ActionDispatch::DebugExceptions, Bugsnag::Rack - rescue - app.config.middleware.use Bugsnag::Rack + begin + app.config.middleware.insert_after ActionDispatch::DebugExceptions, Bugsnag::Rack + rescue + app.config.middleware.use Bugsnag::Rack + end + rescue FrozenError + # This can happen when running RSpec if there is a crash after Rails has + # started booting but before we've added our middleware. If we don't ignore + # this error then the stacktrace blames Bugsnag, which isn't accurate as + # the middleware will only be frozen if an earlier error occurs + # See this comment for more info: + # https://github.com/thoughtbot/factory_bot_rails/issues/303#issuecomment-434560625 + Bugsnag.configuration.warn("Unable to add Bugsnag::Rack middleware as the middleware stack is frozen") end end