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: {}) ⇒ ExceptionMaster

Returns a new instance of ExceptionMaster.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/exception_master.rb', line 8

def initialize(raise_error: true, deliver_email: true, email_config: {})

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

  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



28
29
30
# File 'lib/exception_master.rb', line 28

def deliver_exception(e)
  Pony.mail(@email_config.merge({html_body: error_to_html(e)}))
end

#watchObject



21
22
23
24
25
26
# File 'lib/exception_master.rb', line 21

def watch
  yield
rescue Exception => e
  deliver_exception(e) if @deliver_email
  raise(e) and exit if @raise_error
end