Class: ExceptionNo::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_no.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, notifier, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



71
72
73
74
75
# File 'lib/exception_no.rb', line 71

def initialize(app, notifier, options = {})
  @app = app
  @notifier = notifier
  @sanitizer = options.fetch(:sanitizer, -> _ { _ })
end

Instance Method Details

#call(env) ⇒ Object



77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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