Class: Bugsnag::Middleware::RackRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsnag/middleware/rack_request.rb

Instance Method Summary collapse

Constructor Details

#initialize(bugsnag) ⇒ RackRequest

Returns a new instance of RackRequest.



3
4
5
# File 'lib/bugsnag/middleware/rack_request.rb', line 3

def initialize(bugsnag)
  @bugsnag = bugsnag
end

Instance Method Details

#call(notification) ⇒ Object



7
8
9
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
40
41
42
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
# File 'lib/bugsnag/middleware/rack_request.rb', line 7

def call(notification)
  if notification.request_data[:rack_env]
    env = notification.request_data[:rack_env]

    request = ::Rack::Request.new(env)

    params = request.params rescue {}

    session = env["rack.session"]

    # Set the context
    notification.context = "#{request.request_method} #{request.path}"

    # Set a sensible default for user_id
    notification.user_id = request.ip

    # Build the clean url (hide the port if it is obvious)
    url = "#{request.scheme}://#{request.host}"
    url << ":#{request.port}" unless [80, 443].include?(request.port)
    url << Bugsnag::Helpers.cleanup_url(request.fullpath, notification.configuration.params_filters)

    headers = {}

    env.each_pair do |key, value|
      if key.start_with?("HTTP_")
        header_key = key[5..-1]
      elsif ["CONTENT_TYPE", "CONTENT_LENGTH"].include?(key)
        header_key = key
      else
        next
      end

      headers[header_key.split("_").map {|s| s.capitalize}.join("-")] = value
    end

    # Add a request tab
    notification.add_tab(:request, {
      :url => url,
      :httpMethod => request.request_method,
      :params => params.to_hash,
      :referer => request.referer,
      :clientIp => request.ip,
      :headers => headers
    })

    # Add an environment tab
    if notification.configuration.send_environment
      notification.add_tab(:environment, env)
    end

    # Add a session tab
    if session
      if session.is_a?(Hash)
        # Rails 3
        notification.add_tab(:session, session)
      elsif session.respond_to?(:to_hash)
        # Rails 4
        notification.add_tab(:session, session.to_hash)
      end
    end

    # Add a cookies tab
    cookies = request.cookies
    notification.add_tab(:cookies, cookies) if cookies
  end

  @bugsnag.call(notification)
end