rack-exception_notifier 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9bbcffea7a8294e5955073047dac373156125438
4
+ data.tar.gz: dd3f3a570d8974c93d95108a0bc985e6904b96e0
5
+ SHA512:
6
+ metadata.gz: cb53f2455e04b8317f10f4f15918263fddc9df5fd5a7bf1e843fab07f479bac7ea06d6d046c78636ee20a12baf531c022975ada05951377ff745e25e9abff585
7
+ data.tar.gz: 3ff38c7e83cee7cdc410910ee97eed661ba5b2815d2e3310c3703b306694f116508097b9ea335ef622f48a2803d270320b005df7aff68defd07361ed23a2eb21
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # rack-exception_notifier [![Build Status](https://travis-ci.org/jtdowney/rack-exception_notifier.png?branch=master)](https://travis-ci.org/jtdowney/rack-exception_notifier)
2
+
3
+ Rack-exception_notifier is a piece of simple rack middleware that will rescue exceptions and send email using the mail gem. This gem is based on Rack::MailExceptions in rack-contrib.
4
+
5
+ ## Usage
6
+
7
+ First include rack-exception_notifier in your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-exception_notifier'
11
+ ```
12
+
13
+ Next configure the mail gem, by default mail will send email via SMTP to port 25 on localhost. If you would like to change that you can configure the gem according to [their documentation](https://github.com/mikel/mail/).
14
+
15
+ Then you need to configure rack to use the middleware. Below is an example for sinatra:
16
+
17
+ ```ruby
18
+ use Rack::ExceptionNotifier,
19
+ :to => 'me@example.com',
20
+ :from => 'app@example.com',
21
+ :subject => '[ERROR] %s'
22
+ ```
23
+
24
+ ## License
25
+
26
+ Rack-exception_notifier is released under the [MIT license](http://www.opensource.org/licenses/MIT).
@@ -0,0 +1,73 @@
1
+ module Rack
2
+ class ExceptionNotifier
3
+ def initialize(app, options)
4
+ default_options = {
5
+ :to => nil,
6
+ :from => ENV['USER'] || 'rack@localhost',
7
+ :subject => '[ERROR] %s'
8
+ }
9
+ @app = app
10
+ @options = default_options.merge(options)
11
+ @template = ERB.new(Template)
12
+ end
13
+
14
+ def call(env)
15
+ @app.call(env)
16
+ rescue => e
17
+ _send_notification(e, env)
18
+ raise
19
+ end
20
+
21
+ def _send_notification(exception, env)
22
+ mail = Mail.new
23
+ mail.to(@options[:to])
24
+ mail.from(@options[:from])
25
+ mail.subject(@options[:subject] % [exception.to_s])
26
+ mail.body(@template.result(binding))
27
+ mail.deliver!
28
+ end
29
+
30
+ def _extract_body(env)
31
+ io = env['rack.input']
32
+ io.rewind if io.respond_to?(:rewind)
33
+ contents = io.read
34
+ if contents.empty?
35
+ false
36
+ else
37
+ contents
38
+ end
39
+ end
40
+
41
+ Template = (<<-'EMAIL').gsub(/^ {4}/, '')
42
+ A <%= exception.class.to_s %> occured: <%= exception.to_s %>
43
+ <% if body = _extract_body(env) %>
44
+
45
+ ===================================================================
46
+ Request Body:
47
+ ===================================================================
48
+
49
+ <%= body.gsub(/^/, ' ') %>
50
+ <% end %>
51
+
52
+ ===================================================================
53
+ Rack Environment:
54
+ ===================================================================
55
+
56
+ PID: <%= $$ %>
57
+ PWD: <%= Dir.getwd %>
58
+
59
+ <%= env.to_a.
60
+ sort{|a,b| a.first <=> b.first}.
61
+ map{ |k,v| "%-25s%p" % [k+':', v] }.
62
+ join("\n ") %>
63
+
64
+ <% if exception.respond_to?(:backtrace) %>
65
+ ===================================================================
66
+ Backtrace:
67
+ ===================================================================
68
+
69
+ <%= exception.backtrace.join("\n ") %>
70
+ <% end %>
71
+ EMAIL
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class ExceptionNotifier
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::ExceptionNotifier do
4
+ before(:each) do
5
+ Mail::TestMailer.deliveries.clear
6
+ end
7
+
8
+ let(:good_app) { lambda { |env| [200, {}, ['']] } }
9
+ let(:bad_app) { lambda { |env| raise TestError, 'Test Message' } }
10
+ let(:env) { Rack::MockRequest.env_for("/foo", :method => 'GET') }
11
+ let(:env_with_body) { Rack::MockRequest.env_for("/foo", :method => 'POST', :input => StringIO.new('somethingspecial')) }
12
+
13
+ describe 'call' do
14
+ it 'does not send mail on success' do
15
+ notifier = Rack::ExceptionNotifier.new(good_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s')
16
+ notifier.call(env)
17
+ Mail::TestMailer.deliveries.should be_empty
18
+ end
19
+
20
+ it 'sends mail on exceptions' do
21
+ notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s')
22
+ expect do
23
+ notifier.call(env)
24
+ end.to raise_error(TestError)
25
+
26
+ mail = Mail::TestMailer.deliveries.first
27
+ mail.to.should == ['bar@example.com']
28
+ mail.from.should == ['noreply@example.com']
29
+ mail.subject.should == 'testing - Test Message'
30
+ mail.body.raw_source.should_not include('Request Body')
31
+ end
32
+
33
+ it 'sends mail as user by default' do
34
+ notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :subject => 'testing - %s')
35
+ expect do
36
+ notifier.call(env)
37
+ end.to raise_error(TestError)
38
+
39
+ mail = Mail::TestMailer.deliveries.first
40
+ mail.from.should == [ENV['USER']]
41
+ end
42
+
43
+ it 'includes the body' do
44
+ notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s')
45
+ expect do
46
+ notifier.call(env_with_body)
47
+ end.to raise_error(TestError)
48
+
49
+ mail = Mail::TestMailer.deliveries.first
50
+ mail.body.raw_source.should include('somethingspecial')
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,15 @@
1
+ require 'mail'
2
+ require 'rack/exception_notifier'
3
+ require 'rack/mock'
4
+
5
+ Mail.defaults do
6
+ delivery_method :test
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.order = 'random'
12
+ end
13
+
14
+ class TestError < StandardError
15
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-exception_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Downey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.5.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.5.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 10.0.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 10.0.3
69
+ description: Rack middleware to send email when exceptions are raised
70
+ email:
71
+ - jdowney@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/rack/exception_notifier/version.rb
77
+ - lib/rack/exception_notifier.rb
78
+ - spec/lib/rack/exception_notifier_spec.rb
79
+ - spec/spec_helper.rb
80
+ - README.md
81
+ homepage: http://github.com/jtdowney/rack-exception_notifier
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Rack middleware to send email when exceptions are raised
105
+ test_files:
106
+ - spec/lib/rack/exception_notifier_spec.rb
107
+ - spec/spec_helper.rb