Class: Logster::Middleware::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/logster/middleware/reporter.rb

Constant Summary collapse

PATH_INFO =
"PATH_INFO"
SCRIPT_NAME =
"SCRIPT_NAME"

Instance Method Summary collapse

Constructor Details

#initialize(app, config = {}) ⇒ Reporter

Returns a new instance of Reporter.



10
11
12
13
# File 'lib/logster/middleware/reporter.rb', line 10

def initialize(app, config = {})
  @app = app
  @error_path = Logster.config.subdirectory + '/report_js_error'
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/logster/middleware/reporter.rb', line 15

def call(env)
  Thread.current[Logster::Logger::LOGSTER_ENV] = env

  path = env[PATH_INFO]
  script_name = env[SCRIPT_NAME]

  if script_name && script_name.length > 0
    path = script_name + path
  end

  if path == @error_path

    if !Logster.config.enable_js_error_reporting
      return [403, {}, "Access Denied"]
    end

    Logster.config.current_context.call(env) do
      if Logster.config.rate_limit_error_reporting
        req = Rack::Request.new(env)
        if Logster.store.rate_limited?(req.ip, perform: true)
          return [429, {}, "Rate Limited"]
        end
      end
      report_js_error(env)
    end
    return [200, {}, ["OK"]]
  end

  @app.call(env)
ensure
  Thread.current[Logster::Logger::LOGSTER_ENV] = nil
end

#report_js_error(env) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/logster/middleware/reporter.rb', line 48

def report_js_error(env)
  req = Rack::Request.new(env)

  params = req.params

  message = (params["message"] || "").dup
  message << "\nUrl: " << params["url"] if params["url"]
  message << "\nLine: " << params["line"] if params["line"]
  message << "\nColumn: " << params["column"] if params["column"]
  message << "\nWindow Location: " << params["window_location"] if params["window_location"]

  backtrace = params["stacktrace"] || ""
  Logster.store.report(::Logger::Severity::WARN,
                      "javascript",
                      message,
                      backtrace: backtrace,
                      env: env)

  true
end