diff --git a/docs/usage.rst b/docs/usage.rst index b68697966..392c937aa 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -54,6 +54,30 @@ If you want to report a message rather than an exception you can use the Raven.capture_message("Something went very wrong") +Referencing Events +------------------ + +The client exposes a ``last_event_id`` accessor allowing you to easily +reference the last captured event. This is useful, for example, if you wanted +to show the user a reference on your error page:: + +.. code-block:: ruby + + # somewhere deep in the stack + Raven.capture do + 1 / 0 + end + +Now you can easily expose this to your error handler: + +.. code-block:: ruby + + class ErrorsController < ApplicationController + def internal_server_error + render(:status => 500, :sentry_event_id => Raven.last_event_id) + end + end + Optional Attributes ------------------- diff --git a/lib/raven/base.rb b/lib/raven/base.rb index 23c401e07..2ba9185f8 100644 --- a/lib/raven/base.rb +++ b/lib/raven/base.rb @@ -115,13 +115,17 @@ def capture_type(obj, options = {}) else send_event(evt) end - + Thread.current[:sentry_last_event_id] = evt.id evt end end alias_method :capture_message, :capture_type alias_method :capture_exception, :capture_type + def last_event_id + Thread.current[:sentry_last_event_id] + end + def should_capture?(message_or_exc) if configuration.should_capture configuration.should_capture.call(*[message_or_exc]) diff --git a/spec/raven/raven_spec.rb b/spec/raven/raven_spec.rb index 5c1d2c359..3cde7d5e0 100644 --- a/spec/raven/raven_spec.rb +++ b/spec/raven/raven_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Raven do - let(:event) { double("event") } + let(:event) { double("event", :id => "event_id") } let(:options) { double("options") } before do @@ -245,4 +245,20 @@ def ivars(object) Raven.inject_without(:delayed_job, :railties, :sidekiq, :rack) end end + + describe '.last_event_id' do + let(:message) { "Test message" } + + it 'sends the result of Event.capture_message' do + expect(Raven).to receive(:send_event).with(event) + + Raven.capture_message("Test message", options) + + expect(Raven.last_event_id).to eq(event.id) + end + + it 'yields the event to a passed block' do + expect { |b| Raven.capture_message(message, options, &b) }.to yield_with_args(event) + end + end end