diff --git a/lib/opbeat.rb b/lib/opbeat.rb index 18c4a95..b6a1449 100644 --- a/lib/opbeat.rb +++ b/lib/opbeat.rb @@ -101,6 +101,17 @@ def capture_exception(exception, options={}) end end + def capture_rack_exception(exception, env, options={}) + exception.set_backtrace caller unless exception.backtrace + if (evt = Event.from_rack_exception(exception, env, options)) + if self.configuration.async? + self.configuration.async.call(evt) + else + send(evt) + end + end + end + def capture_message(message, options={}) if (evt = Event.from_message(message, caller, options)) if self.configuration.async? diff --git a/lib/opbeat/rack.rb b/lib/opbeat/rack.rb index 3ecba76..377e3a4 100644 --- a/lib/opbeat/rack.rb +++ b/lib/opbeat/rack.rb @@ -28,16 +28,14 @@ def call(env) rescue Error => e raise # Don't capture Opbeat errors rescue Exception => e - evt = Event.from_rack_exception(e, env) - Opbeat.send(evt) + Opbeat.capture_rack_exception(e, env) raise end error = env['rack.exception'] || env['sinatra.error'] if error - evt = Event.from_rack_exception(error, env) - Opbeat.send(evt) if evt + Opbeat.capture_rack_exception(error, env) end response diff --git a/opbeat.gemspec b/opbeat.gemspec index eda1c7e..afb8b1f 100644 --- a/opbeat.gemspec +++ b/opbeat.gemspec @@ -17,7 +17,7 @@ Gem::Specification.new do |gem| gem.extra_rdoc_files = ["README.md", "LICENSE"] gem.add_dependency "faraday", [">= 0.7.6", "< 0.10"] - gem.add_dependency "multi_json", "~> 1.0" + gem.add_dependency "multi_json", "~> 1.12.1" gem.add_development_dependency "bundler", "~> 1.7" gem.add_development_dependency "rake", "~> 10.0" diff --git a/spec/opbeat/opbeat_spec.rb b/spec/opbeat/opbeat_spec.rb index 1c83007..50c18d4 100644 --- a/spec/opbeat/opbeat_spec.rb +++ b/spec/opbeat/opbeat_spec.rb @@ -8,6 +8,7 @@ allow(Opbeat).to receive(:send) { @send } allow(Opbeat::Event).to receive(:from_message) { @event } allow(Opbeat::Event).to receive(:from_exception) { @event } + allow(Opbeat::Event).to receive(:from_rack_exception) { @event } end it 'capture_message should send result of Event.from_message' do @@ -36,7 +37,31 @@ Opbeat.capture_exception(exception) end + it 'capture_rack_exception should send result of Event.from_exception built with env and default options' do + exception = build_exception() + rack_env = build_rack_env() + + expect(Opbeat::Event).to receive(:from_rack_exception).with(exception, rack_env, {}) + expect(Opbeat).to receive(:send).with(@event) + + Opbeat.capture_rack_exception(exception, rack_env) + end + + it "capture_rack_exception should send result of Event.from_exception built with env and options" do + exception = build_exception() + rack_env = build_rack_env() + + expect(Opbeat::Event).to receive(:from_rack_exception).with(exception, rack_env, {:custom => :param}) + expect(Opbeat).to receive(:send).with(@event) + + Opbeat.capture_rack_exception(exception, rack_env, {:custom => :param}) + end + context "async" do + after do + Opbeat.configuration.async = false + end + it 'capture_message should send result of Event.from_message' do async = lambda {} message = "Test message" diff --git a/spec/opbeat/rack_spec.rb b/spec/opbeat/rack_spec.rb index fef0c0b..f2236af 100644 --- a/spec/opbeat/rack_spec.rb +++ b/spec/opbeat/rack_spec.rb @@ -35,14 +35,13 @@ def custom_user @send = double("send") @event = double("event") allow(Opbeat).to receive(:send) { @send } - allow(Opbeat::Event).to receive(:from_rack_exception) { @event } end it 'should capture exceptions' do exception = build_exception() env = {} - - expect(Opbeat::Event).to receive(:from_rack_exception).with(exception, env) + + expect(Opbeat::Event).to receive(:from_rack_exception).with(exception, env, {}) { @event } expect(Opbeat).to receive(:send).with(@event) app = lambda do |e| @@ -57,7 +56,7 @@ def custom_user exception = build_exception() env = {} - expect(Opbeat::Event).to receive(:from_rack_exception).with(exception, env) + expect(Opbeat::Event).to receive(:from_rack_exception).with(exception, env, {}) { @event } expect(Opbeat).to receive(:send).with(@event) app = lambda do |e| diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c02633b..3c80c89 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,3 +8,15 @@ def build_exception() return exception end end + +def build_rack_env() + { + "QUERY_STRING" => "a=1&b=2", + "REMOTE_ADDR" => "::1", + "REMOTE_HOST" => "localhost", + "REQUEST_METHOD" => "GET", + "REQUEST_PATH" => "/index.html", + "HTTP_HOST" => "localhost:3000", + "HTTP_VERSION" => "HTTP/1.1" + } +end