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.



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

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

Instance Method Details

#call(env) ⇒ Object



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

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

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

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

  if path == @error_path
    return 403, {}, ["Access Denied"] if !Logster.config.enable_js_error_reporting

    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



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

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"] || ""

  severity = ::Logger::Severity::WARN
  if params["severity"] && ::Logger::Severity.const_defined?(params["severity"].upcase)
    severity = ::Logger::Severity.const_get(params["severity"].upcase)
  end

  Logster.store.report(severity, "javascript", message, backtrace: backtrace, env: env)

  true
end