module Raven
Inspired by Rails’ and Airbrake’s backtrace parsers.
A much simpler source line cacher because linecache sucks at platform compat
Some parts adapted from golang.org/src/pkg/json/decode.go and golang.org/src/pkg/utf8/utf8.go
Constants
- INTERFACES
- VERSION
Attributes
The client object is responsible for delivering formatted data to the Sentry server. Must respond to send. See Raven::Client
.
A Raven
configuration object. Must act like a hash and return sensible values for all Raven
configuration options. See Raven::Configuration
.
Public Class Methods
Provides extra context to the exception prior to it being handled by Raven
. An exception can have multiple annotations, which are merged together.
The options (annotation) is treated the same as the “options“ parameter to “capture_exception“ or “Event.from_exception“, and can contain the same “:user“, “:tags“, etc. options as these methods.
These will be merged with the “options“ parameter to “Event.from_exception“ at the top of execution.
@example
begin raise "Hello" rescue => exc Raven.annotate_exception(exc, :user => { 'id' => 1, 'email' => 'foo@example.com' }) end
# File lib/raven/base.rb, line 162 def annotate_exception(exc, options = {}) notes = exc.instance_variable_get(:@__raven_context) || {} notes.merge!(options) exc.instance_variable_set(:@__raven_context, notes) exc end
Capture and process any exceptions from the given block, or globally if no block is given
@example
Raven.capture do MyApp.run end
# File lib/raven/base.rb, line 82 def capture(options = {}, &block) if block begin block.call rescue Error raise # Don't capture Raven errors rescue Exception => e capture_exception(e, options) raise end else # Install at_exit hook at_exit do if $ERROR_INFO logger.debug "Caught a post-mortem exception: #{$ERROR_INFO.inspect}" capture_exception($ERROR_INFO, options) end end end end
# File lib/raven/base.rb, line 103 def capture_exception(exception, options = {}) send_or_skip(exception) do if evt = Event.from_exception(exception, options) yield evt if block_given? if configuration.async? configuration.async.call(evt) else send(evt) end end end end
# File lib/raven/base.rb, line 116 def capture_message(message, options = {}) send_or_skip(message) do if evt = Event.from_message(message, options) yield evt if block_given? if configuration.async? configuration.async.call(evt) else send(evt) end end end end
The client object is responsible for delivering formatted data to the Sentry server.
# File lib/raven/base.rb, line 43 def client @client ||= Client.new(configuration) end
The configuration object. @see Raven.configure
# File lib/raven/base.rb, line 38 def configuration @configuration ||= Configuration.new end
Call this method to modify defaults in your initializers.
@example
Raven.configure do |config| config.server = 'http://...' end
# File lib/raven/base.rb, line 58 def configure(silent = false) yield(configuration) if block_given? self.client = Client.new(configuration) report_ready unless silent self.client end
# File lib/raven/base.rb, line 28 def context Context.current end
Bind extra context. Merges with existing context (if any).
Extra context shows up as Additional Data within Sentry, and is completely arbitrary.
@example
Raven.tags_context('my_custom_data' => 'value')
# File lib/raven/base.rb, line 197 def extra_context(options = {}) self.context.extra.merge!(options) end
# File lib/raven/interfaces.rb, line 31 def self.find_interface(name) INTERFACES[name.to_s] end
Injects various integrations
# File lib/raven/base.rb, line 209 def inject require 'raven/integrations/delayed_job' if defined?(::Delayed::Plugin) require 'raven/railtie' if defined?(::Rails::Railtie) require 'raven/sidekiq' if defined?(Sidekiq) if defined?(Rake) require 'raven/rake' require 'raven/tasks' end end
# File lib/raven/base.rb, line 32 def logger @logger ||= Logger.new end
# File lib/raven/base.rb, line 201 def rack_context(env) if env.empty? env = nil end self.context.rack_env = env end
# File lib/raven/interfaces.rb, line 24 def self.register_interface(mapping) mapping.each_pair do |key, klass| INTERFACES[key.to_s] = klass INTERFACES[klass.name] = klass end end
Tell the log that the client is good to go
# File lib/raven/base.rb, line 48 def report_ready self.logger.info "Raven #{VERSION} ready to catch errors" end
Send an event to the configured Sentry server
@example
evt = Raven::Event.new(:message => "An error") Raven.send(evt)
# File lib/raven/base.rb, line 71 def send(evt) client.send(evt) end
# File lib/raven/base.rb, line 129 def send_or_skip(exc) should_send = if configuration.should_send configuration.should_send.call(*[exc]) else true end if configuration.send_in_current_environment? && should_send yield if block_given? else configuration.log_excluded_environment_message end end
Bind user context. Merges with existing context (if any).
It is recommending that you send at least the “id“ and “email“ values. All other values are arbitrary.
@example
Raven.user_context('id' => 1, 'email' => 'foo@example.com')
# File lib/raven/base.rb, line 176 def user_context(options = {}) self.context.user = options end