class ExceptionMaster

Attributes

email_config[R]

Public Class Methods

new(raise_error: true, deliver_email: true, email_config: {}, environment: 'development', send_when_environment_is: ['production', 'staging']) click to toggle source
# File lib/exception_master.rb, line 8
def initialize(raise_error: true, deliver_email: true, email_config: {}, environment: 'development', send_when_environment_is: ['production', 'staging'])

  @email_config  = { via: :sendmail, from: 'exception-master@localhost', subject: 'Error' }
  @email_config.merge!(email_config)
  @raise_error   = raise_error
  @deliver_email = deliver_email
  @environment   = environment
  @send_when_environment_is = send_when_environment_is

  if @email_config[:to].nil?
    raise "Please specify email addresses of email recipients using :to key in email_config attr (value should be array)"
  end

end

Public Instance Methods

deliver_exception(e) click to toggle source
# File lib/exception_master.rb, line 36
def deliver_exception(e)
  if @send_when_environment_is.include?(@environment)
    Pony.mail(@email_config.merge({html_body: error_to_html(e)}))
  end
end
watch() { || ... } click to toggle source
# File lib/exception_master.rb, line 23
def watch
  yield
rescue Exception => e
  deliver_exception(e) if @deliver_email
  if @raise_error
    raise(e) and exit 
  elsif @environment == 'development'
    puts "\n\nException raised: " + e.inspect
    puts "\nTraceback:\n"
    e.backtrace.each { |line| puts "   " + line }
  end
end

Private Instance Methods

error_to_html(e) click to toggle source
# File lib/exception_master.rb, line 45
def error_to_html(e)
  template = ERB.new(File.read(File.expand_path(File.dirname(__FILE__)) + '/../views/error.html.erb'))
  template.result(binding)
end