class ExceptionNo

Constants

TEMPLATE
VERSION

Attributes

backtrace_filter[RW]
behaviors[RW]

Public Class Methods

new(config = {}) click to toggle source
# File lib/exception_no.rb, line 13
def initialize(config = {})
  @config = config
  @template = ERB.new(TEMPLATE)
  @behaviors = [:deliver]

  @backtrace_filter = -> line { true }
end

Public Instance Methods

_deliver(exception, env = {}) click to toggle source
# File lib/exception_no.rb, line 21
def _deliver(exception, env = {})
  body = @template.result(binding)

  Net::SMTP.start(@config.fetch(:host), @config.fetch(:port, 25)) do |smtp|
    smtp.send_message(body, @config.fetch(:from), @config.fetch(:to))
  end
end
deliver(exception, options) click to toggle source
# File lib/exception_no.rb, line 29
def deliver(exception, options)
  begin
    _deliver(exception, options)
  rescue => notification_error
    $stderr.write("*** FAILED SENDING ERROR NOTIFICATION\n")
    $stderr.write("*** #{notification_error.class}: #{notification_error}\n")
    $stderr.write("*** #{exception.class}: #{exception.message}\n")

    exception.backtrace.each do |line|
      $stderr.write("*** #{line}\n")
    end
  end
end
notify(exception, options = {}) click to toggle source
# File lib/exception_no.rb, line 43
def notify(exception, options = {})
  deliver(exception, options) if @behaviors.include?(:deliver)
  raise exception if @behaviors.include?(:raise)
end
run(env = {}) { || ... } click to toggle source
# File lib/exception_no.rb, line 48
def run(env = {})
  begin
    yield
  rescue Exception => ex
    notify(ex, env)
  end
end