class ExceptionNo::Middleware

Public Class Methods

new(app, notifier, options = {}) click to toggle source
# File lib/exception_no.rb, line 71
def initialize(app, notifier, options = {})
  @app = app
  @notifier = notifier
  @sanitizer = options.fetch(:sanitizer, -> _ { _ })
end

Public Instance Methods

call(env) click to toggle source
# File lib/exception_no.rb, line 77
def call(env)
  begin
    @app.call(env)
  rescue Exception => e
    @notifier.notify(e, extract_env(env))

    raise e
  end
end
extract_env(env) click to toggle source
# File lib/exception_no.rb, line 87
def extract_env(env)
  req = Rack::Request.new(env)

  parts = []

  parts << "#{req.request_method} #{req.url}"
  parts << "User-Agent: #{req.user_agent}" if req.user_agent
  parts << "Referrer: #{req.referrer}" if req.referrer
  parts << "IP: #{req.ip}" if req.ip
  parts << "Cookie: #{req.env["HTTP_COOKIE"]}" if req.cookies.size > 0

  if req.form_data?
    body = @sanitizer.call(req.POST).pretty_inspect
  else
    req.body.rewind

    body = req.body.read

    if body.empty?
      body = nil
    else
      body = @sanitizer.call(body)
    end
  end

  if body
    parts << "Body: \n\n#{body.gsub(/^/, "  ")}"
  end

  parts
end