Class: ExceptionMaster

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_master.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raise_error: true, deliver_email: true, email_config: {}, environment: 'development', send_when_environment_is: ['production', 'staging']) ⇒ ExceptionMaster

Returns a new instance of ExceptionMaster.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#email_configObject (readonly)

Returns the value of attribute email_config.



6
7
8
# File 'lib/exception_master.rb', line 6

def email_config
  @email_config
end

Instance Method Details

#deliver_exception(e) ⇒ Object



36
37
38
39
40
# 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

#watchObject



23
24
25
26
27
28
29
30
31
32
33
34
# 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