Skip to content

Commit

Permalink
Supress frozen middleware errors in Railtie
Browse files Browse the repository at this point in the history
This should never happen in normal operation, but is possible when
running RSpec tests

See thoughtbot/factory_bot_rails#303 (comment)
and #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
  • Loading branch information
imjoehaines committed Jul 23, 2020
1 parent f81c855 commit 885be37
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/bugsnag/integrations/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 885be37

Please sign in to comment.