/
rack.rb
57 lines (52 loc) · 1.37 KB
/
rack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Honeybadger
# Middleware for Rack applications. Any errors raised by the upstream
# application will be delivered to Honeybadger and re-raised.
#
# Synopsis:
#
# require 'rack'
# require 'honeybadger'
#
# Honeybadger.configure do |config|
# config.api_key = 'my_api_key'
# end
#
# app = Rack::Builder.app do
# run lambda { |env| raise "Rack down" }
# end
#
# use Honeybadger::Rack
# run app
#
# Use a standard Honeybadger.configure call to configure your api key.
class Rack
def initialize(app)
@app = app
end
def ignored_user_agent?(env)
true if Honeybadger.
configuration.
ignore_user_agent.
flatten.
any? { |ua| ua === env['HTTP_USER_AGENT'] }
end
def notify_honeybadger(exception,env)
Honeybadger.notify_or_ignore(exception, :rack_env => env) unless ignored_user_agent?(env)
end
def call(env)
begin
response = @app.call(env)
rescue Exception => raised
env['honeybadger.error_id'] = notify_honeybadger(raised, env)
raise
ensure
Honeybadger.context.clear!
end
framework_exception = env['rack.exception'] || env['sinatra.error']
if framework_exception
env['honeybadger.error_id'] = notify_honeybadger(framework_exception, env)
end
response
end
end
end