Class: ExceptionNo

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

Defined Under Namespace

Classes: Middleware

Constant Summary collapse

VERSION =
"0.0.3"
TEMPLATE =
(<<-'EMAIL').gsub(/^ {2}/, '')
From: <%= @config[:from_alias] %> <<%= @config[:from] %>>
To: <<%= @config[:to] %>>
Subject: <%= exception.class %>: <%= exception.message.split.join(" ") %>

<%= options[:body] %>

<%= "~" * 80 %>

A <%= exception.class.to_s %> occured: <%= exception.to_s %>

<%= exception.backtrace.select { |line| @backtrace_filter.call(line) }.join("\n") if exception.backtrace %>
EMAIL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ ExceptionNo

Returns a new instance of ExceptionNo.



10
11
12
13
14
15
16
# File 'lib/exception_no.rb', line 10

def initialize(config = {})
  @config = config
  @template = ERB.new(TEMPLATE)
  @deliver = true

  @backtrace_filter = -> line { true }
end

Instance Attribute Details

#backtrace_filterObject

Returns the value of attribute backtrace_filter.



7
8
9
# File 'lib/exception_no.rb', line 7

def backtrace_filter
  @backtrace_filter
end

#deliverObject

Returns the value of attribute deliver.



8
9
10
# File 'lib/exception_no.rb', line 8

def deliver
  @deliver
end

Instance Method Details

#_notify(exception, options = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/exception_no.rb', line 18

def _notify(exception, options = {})
  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

#notify(exception, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/exception_no.rb', line 26

def notify(exception, options = {})
  return unless @deliver

  begin
    _notify(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