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

client[W]

The client object is responsible for delivering formatted data to the Sentry server. Must respond to send. See Raven::Client.

configuration[W]

A Raven configuration object. Must act like a hash and return sensible values for all Raven configuration options. See Raven::Configuration.

Public Class Methods

annotate(exc, options = {})
Alias for: annotate_exception
annotateException(exc, options = {})
Alias for: annotate_exception
annotate_exception(exc, options = {}) click to toggle source

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
Also aliased as: annotateException, annotate
capture(options = {}, &block) click to toggle source

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
captureException(exception, options = {})

For cross-language compat

Alias for: capture_exception
captureMessage(message, options = {})
Alias for: capture_message
capture_exception(exception, options = {}) { |evt| ... } click to toggle source
# 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
Also aliased as: captureException
capture_message(message, options = {}) { |evt| ... } click to toggle source
# 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
Also aliased as: captureMessage
client() click to toggle source

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
configuration() click to toggle source

The configuration object. @see Raven.configure

# File lib/raven/base.rb, line 38
def configuration
  @configuration ||= Configuration.new
end
configure(silent = false) { |configuration| ... } click to toggle source

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
context() click to toggle source
# File lib/raven/base.rb, line 28
def context
  Context.current
end
extra_context(options = {}) click to toggle source

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
find_interface(name) click to toggle source
# File lib/raven/interfaces.rb, line 31
def self.find_interface(name)
  INTERFACES[name.to_s]
end
inject() click to toggle source

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
logger() click to toggle source
# File lib/raven/base.rb, line 32
def logger
  @logger ||= Logger.new
end
rack_context(env) click to toggle source
# File lib/raven/base.rb, line 201
def rack_context(env)
  if env.empty?
    env = nil
  end
  self.context.rack_env = env
end
register_interface(mapping) click to toggle source
# 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
report_ready() click to toggle source

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(evt) click to toggle source

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
send_or_skip(exc) { || ... } click to toggle source
# 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
tags_context(options = {}) click to toggle source

Bind tags context. Merges with existing context (if any).

Tags are key / value pairs which generally represent things like application version, environment, role, and server names.

@example

Raven.tags_context('my_custom_tag' => 'tag_value')
# File lib/raven/base.rb, line 187
def tags_context(options = {})
  self.context.tags.merge!(options)
end
user_context(options = {}) click to toggle source

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