Class: Bugsnag::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsnag/integrations/rack.rb

Overview

Automatically captures and adds Rack request information to error reports

Constant Summary collapse

FRAMEWORK_ATTRIBUTES =
{
  :framework => "Rack"
}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bugsnag/integrations/rack.rb', line 10

def initialize(app)
  @app = app

  # Configure bugsnag rack defaults
  Bugsnag.configure do |config|
    # Try to set the release_stage automatically if it hasn't already been set
    config.release_stage ||= ENV["RACK_ENV"] if ENV["RACK_ENV"]

    # Try to set the project_root if it hasn't already been set, or show a warning if we can't
    unless config.project_root && !config.project_root.to_s.empty?
      if defined?(settings)
        config.project_root = settings.root
      else
        config.warn("You should set your app's project_root (see https://docs.bugsnag.com/platforms/ruby/rails/configuration-options/#project_root).")
      end
    end

    # Hook up rack-based notification middlewares
    config.internal_middleware.insert_before(Bugsnag::Middleware::Rails3Request, Bugsnag::Middleware::RackRequest) if defined?(::Rack)
    config.internal_middleware.use(Bugsnag::Middleware::WardenUser) if defined?(Warden)
    config.internal_middleware.use(Bugsnag::Middleware::ClearanceUser) if defined?(Clearance)

    # Set environment data for payload
    # Note we only set the detected app_type if it's not already set. This
    # ensures we don't overwrite the value set by the Railtie
    config.detected_app_type ||= "rack"
    config.runtime_versions["rack"] = ::Rack.release if defined?(::Rack)
    config.runtime_versions["sinatra"] = ::Sinatra::VERSION if defined?(::Sinatra)
  end
end

Instance Method Details

#call(env) ⇒ Object

Wraps a call to the application with error capturing


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bugsnag/integrations/rack.rb', line 43

def call(env)
  # Set the request data for bugsnag middleware to use
  Bugsnag.configuration.set_request_data(:rack_env, env)
  if Bugsnag.configuration.auto_capture_sessions
    Bugsnag.start_session
  end

  begin
    response = @app.call(env)
  rescue Exception => raised
    # Notify bugsnag of rack exceptions
    Bugsnag.notify(raised, true) do |report|
      report.severity = "error"
      report.severity_reason = {
        :type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
        :attributes => Bugsnag::Rack::FRAMEWORK_ATTRIBUTES
      }
    end

    # Re-raise the exception
    raise
  end

  # Notify bugsnag of rack exceptions
  if env["rack.exception"]
    Bugsnag.notify(env["rack.exception"], true) do |report|
      report.severity = "error"
      report.severity_reason = {
        :type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
        :attributes => FRAMEWORK_ATTRIBUTES
      }
    end
  end

  response
ensure
  # Clear per-request data after processing the each request
  Bugsnag.configuration.clear_request_data
end