Class: Logster::Middleware::Reporter

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

Constant Summary collapse

PATH_INFO =
"PATH_INFO".freeze
SCRIPT_NAME =
"SCRIPT_NAME".freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Reporter.



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

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

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logster/middleware/reporter.rb', line 13

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
    Logster.config.current_context.call(env) do
      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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/logster/middleware/reporter.rb', line 36

def report_js_error(env)
  req = Rack::Request.new(env)
  message = req["message"] || ""
  message << "\nUrl: " << req["url"] if req["url"]
  message << "\nLine: " << req["line"] if req["line"]
  message << "\nColumn: " << req["column"] if req["column"]
  message << "\nWindow Location: " << req["window_location"] if req["window_location"]

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