module Trouble

A generic wrapper to report errors and Exceptions.

Public Class Methods

config() click to toggle source

Public: Returns the the configuration instance.

# File lib/trouble/configuration.rb, line 28
def self.config
  @config ||= Configuration.new
end
configure() { |config| ... } click to toggle source

Public: Yields the configuration instance.

# File lib/trouble/configuration.rb, line 34
def self.configure(&block)
  yield config
end
log(exception, metadata = nil) click to toggle source

Public: Log the error in the logger and track as metric.

exception - An instance of an Exception metadata - An Hash with arbitrary additional information (optional)

Examples

Trouble.log RuntimeError.new
Trouble.log RuntimeError.new, some_idea_why_it_happened: "I don't know, but try this and that."

Returns nothing.

# File lib/trouble.rb, line 39
def self.log(exception, metadata = nil)
  exception.set_backtrace(caller) unless exception.backtrace
  log_in_logger(exception, metadata)
  increment_metric
end
notify(exception, metadata = nil) click to toggle source

Public: Notify the developers about an Exception

exception - An instance of an Exception metadata - An Hash with arbitrary additional information (optional)

Examples

Trouble.notify RuntimeError.new
Trouble.notify RuntimeError.new, some_idea_why_it_happened: "I don't know, but try this and that."

Returns nothing.

# File lib/trouble.rb, line 20
def self.notify(exception, metadata = nil)
  exception.set_backtrace(caller) unless exception.backtrace
  notify_error_service(exception, metadata)
  log_in_logger(exception, metadata)
  increment_metric
end
reset!() click to toggle source

Public: Reset the configuration (useful for testing)

# File lib/trouble/configuration.rb, line 40
def self.reset!
  @config = nil
end

Private Class Methods

increment_metric() click to toggle source

Internal: track exceptions metric

# File lib/trouble.rb, line 55
def self.increment_metric
  Meter.increment('exceptions') if defined?(Meter)
end
log_in_logger(exception, metadata) click to toggle source

Internal: Log to the current Logger.

# File lib/trouble.rb, line 61
def self.log_in_logger(exception, metadata)
  config.logger.error("TROUBLE LOG #{exception.inspect} at #{exception.backtrace.first} with metadata #{metadata.inspect}") if config.logger
end
notify_error_service(exception, metadata) click to toggle source

Internal: Dispatch the Exception to the backend(s).

# File lib/trouble.rb, line 49
def self.notify_error_service(exception, metadata)
  Bugsnag.notify(exception, metadata) if defined?(Bugsnag)
end