Sha256: 3c773d6015b6d8cc1e9361d3599f6040ab6567b22585db0ac2cd933c49fb6c9f

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

module Logster
  module Middleware
    class Reporter

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

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

      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

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

        message = params["message"] || ""
        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)
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
logster-2.1.2 lib/logster/middleware/reporter.rb
logster-2.1.0 lib/logster/middleware/reporter.rb
logster-2.0.1 lib/logster/middleware/reporter.rb
logster-2.0.0.pre lib/logster/middleware/reporter.rb