public
Fork of rtomayko/rack-contrib
Description: Contributed Rack Middleware and Utilities
Homepage:
Clone URL: git://github.com/rack/rack-contrib.git
rack-contrib / lib / rack / contrib / mailexceptions.rb
100644 121 lines (101 sloc) 3.324 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'net/smtp'
require 'tmail'
require 'erb'
 
module Rack
  # Catches all exceptions raised from the app it wraps and
  # sends a useful email with the exception, stacktrace, and
  # contents of the environment.
 
  class MailExceptions
    attr_reader :config
 
    def initialize(app)
      @app = app
      @config = {
        :to => nil,
        :from => ENV['USER'] || 'rack',
        :subject => '[exception] %s',
        :smtp => {
          :server => 'localhost',
          :domain => 'localhost',
          :port => 25,
          :authentication => :login,
          :user_name => nil,
          :password => nil
        }
      }
      @template = ERB.new(TEMPLATE)
      yield self if block_given?
    end
 
    def call(env)
      status, headers, body =
        begin
          @app.call(env)
        rescue => boom
          # TODO don't allow exceptions from send_notification to
          # propogate
          send_notification boom, env
          raise
        end
      send_notification env['mail.exception'], env if env['mail.exception']
      [status, headers, body]
    end
 
    %w[to from subject].each do |meth|
      define_method(meth) { |value| @config[meth.to_sym] = value }
    end
 
    def smtp(settings={})
      @config[:smtp].merge! settings
    end
 
  private
    def generate_mail(exception, env)
      mail = TMail::Mail.new
      mail.to = Array(config[:to])
      mail.from = config[:from]
      mail.subject = config[:subject] % [exception.to_s]
      mail.date = Time.now
      mail.set_content_type 'text/plain'
      mail.charset = 'UTF-8'
      mail.body = @template.result(binding)
      mail
    end
 
    def send_notification(exception, env)
      mail = generate_mail(exception, env)
      smtp = config[:smtp]
      env['mail.sent'] = true
      return if smtp[:server] == 'example.com'
 
      Net::SMTP.start smtp[:server], smtp[:port], smtp[:domain], smtp[:user_name], smtp[:password], smtp[:authentication] do |server|
        mail.to.each do |recipient|
          server.send_message mail.to_s, mail.from, recipient
        end
      end
    end
 
    def extract_body(env)
      if io = env['rack.input']
        io.rewind if io.respond_to?(:rewind)
        io.read
      end
    end
 
    TEMPLATE = (<<-'EMAIL').gsub(/^ {4}/, '')
A <%= exception.class.to_s %> occured: <%= exception.to_s %>
<% if body = extract_body(env) %>
 
===================================================================
Request Body:
===================================================================
 
<%= body.gsub(/^/, ' ') %>
<% end %>
 
===================================================================
Rack Environment:
===================================================================
 
PID: <%= $$ %>
PWD: <%= Dir.getwd %>
 
<%= env.to_a.
sort{|a,b| a.first <=> b.first}.
map{ |k,v| "%-25s%p" % [k+':', v] }.
join("\n ") %>
 
<% if exception.respond_to?(:backtrace) %>
===================================================================
Backtrace:
===================================================================
 
<%= exception.backtrace.join("\n ") %>
<% end %>
EMAIL
 
  end
end