github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

rack / rack-contrib forked from rtomayko/rack-contrib

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 441
    • 3
  • Source
  • Commits
  • Network (3)
  • Issues (4)
  • Downloads (1)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (1)
    • 0.9.0
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Contributed Rack Middleware and Utilities — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Revert "Rack::ETag now supports SHA-1 and SHA-2 in addition to MD5" 
rtomayko (author)
Tue Feb 09 08:17:28 -0800 2010
commit  e205d2933a749a01af1582df2a1e451a3c1dbb66
tree    b6a4443d08a1ae86b9313b320efc01724f1ec4c7
parent  7390e3f0d3caadbdd31d70d31e99380d201e563d
rack-contrib / lib / rack / contrib / mailexceptions.rb lib/rack/contrib/mailexceptions.rb
100644 121 lines (101 sloc) 3.324 kb
edit raw blame history
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
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server