Skip to content

Instantly share code, notes, and snippets.

@mperham
Created August 17, 2009 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mperham/169286 to your computer and use it in GitHub Desktop.
Save mperham/169286 to your computer and use it in GitHub Desktop.
require "merb-mailer/mailer"
require 'socket'
Merb::Mailer.delivery_method = :sendmail
class ErrorMailer
def self.template(name, error, context)
data = <<-EOF
I regret to inform you that your daemon, #{name}, caught an error at #{Time.now} while running on #{Socket.gethostname}.
Its last words were:
#{error.class.name}: #{error.message}
Context:
#{context.inspect}
Backtrace:
#{error.backtrace.join("\n")}
My condolences.
Sincerely,
ErrorNotifier
EOF
end
def self.deliver_condolences(name, err, ctx)
m = Merb::Mailer.new :to => 'alerts@onespot.com',
:from => 'Error Notifier <notifier@onespot.com>',
:subject => "[#{name}] #{err.message}",
:text => template(name, err, ctx)
m.deliver!
end
end
def notify_upon_exception(name, context={})
begin
yield context
rescue Exception => e
ErrorMailer.deliver_condolences(name, e, context) if RAILS_ENV == 'production' || RAILS_ENV == 'staging'
Rails.logger.error(e.message)
e.backtrace.each { |line| Rails.logger.error(line) }
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment