rack-exception_notifier 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/lib/rack/exception_notifier.rb +17 -9
- data/lib/rack/exception_notifier/version.rb +1 -1
- data/spec/lib/rack/exception_notifier_spec.rb +30 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d74186879da5241fb7327492cb6374f6b5172360
|
4
|
+
data.tar.gz: f2c670e0354515d0ed102d6c7c874ec4fd74194a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a5c62ac24cb6a9cf0d63472119caf7fdd847f9b001621aa3a9451e94ce4b2b5dc4421456d2bc5fff60923524e57a78c768743b40c2d16e0249ebcae94448d4e
|
7
|
+
data.tar.gz: 6a4de62c65be5c9ce3a4c139209a00e8fff01dd552857d048c85e5e44852efa1546a61cda1c61035fd4cfdc32cf97859943a0b66c6477e792b6c6ddf88e4583e
|
data/README.md
CHANGED
@@ -22,6 +22,13 @@ use Rack::ExceptionNotifier,
|
|
22
22
|
:subject => '[ERROR] %s'
|
23
23
|
```
|
24
24
|
|
25
|
+
## Options
|
26
|
+
|
27
|
+
* `:to` - destination for email exceptions (required)
|
28
|
+
* `:from` - address to originate email exceptions from (default: running username)
|
29
|
+
* `:subject` - subject line for emails, %s will be interpolated with the exception #to_s (default: [ERROR] %s)
|
30
|
+
* `:include_body` - should the body of the request be included in the email (default: false)
|
31
|
+
|
25
32
|
## License
|
26
33
|
|
27
34
|
Rack-exception_notifier is released under the [MIT license](http://www.opensource.org/licenses/MIT).
|
@@ -7,11 +7,16 @@ module Rack
|
|
7
7
|
default_options = {
|
8
8
|
:to => nil,
|
9
9
|
:from => ENV['USER'] || 'rack@localhost',
|
10
|
-
:subject => '[ERROR] %s'
|
10
|
+
:subject => '[ERROR] %s',
|
11
|
+
:include_body => false
|
11
12
|
}
|
12
13
|
@app = app
|
13
14
|
@options = default_options.merge(options)
|
14
15
|
@template = ERB.new(Template)
|
16
|
+
|
17
|
+
if @options[:to].nil?
|
18
|
+
raise ArgumentError.new('to address is required')
|
19
|
+
end
|
15
20
|
end
|
16
21
|
|
17
22
|
def call(env)
|
@@ -30,26 +35,29 @@ module Rack
|
|
30
35
|
mail.deliver!
|
31
36
|
end
|
32
37
|
|
38
|
+
def _body_present?(env)
|
39
|
+
env['rack.input'].size > 0
|
40
|
+
end
|
41
|
+
|
42
|
+
def _render_body?(env)
|
43
|
+
_body_present?(env) && @options[:include_body]
|
44
|
+
end
|
45
|
+
|
33
46
|
def _extract_body(env)
|
34
47
|
io = env['rack.input']
|
35
48
|
io.rewind if io.respond_to?(:rewind)
|
36
|
-
|
37
|
-
if contents.empty?
|
38
|
-
false
|
39
|
-
else
|
40
|
-
contents
|
41
|
-
end
|
49
|
+
io.read
|
42
50
|
end
|
43
51
|
|
44
52
|
Template = (<<-'EMAIL').gsub(/^ {4}/, '')
|
45
53
|
A <%= exception.class.to_s %> occured: <%= exception.to_s %>
|
46
|
-
<% if
|
54
|
+
<% if _render_body?(env) %>
|
47
55
|
|
48
56
|
===================================================================
|
49
57
|
Request Body:
|
50
58
|
===================================================================
|
51
59
|
|
52
|
-
<%=
|
60
|
+
<%= _extract_body(env).gsub(/^/, ' ') %>
|
53
61
|
<% end %>
|
54
62
|
|
55
63
|
===================================================================
|
@@ -10,6 +10,14 @@ describe Rack::ExceptionNotifier do
|
|
10
10
|
let(:env) { Rack::MockRequest.env_for("/foo", :method => 'GET') }
|
11
11
|
let(:env_with_body) { Rack::MockRequest.env_for("/foo", :method => 'POST', :input => StringIO.new('somethingspecial')) }
|
12
12
|
|
13
|
+
describe 'initialize' do
|
14
|
+
it 'requires a to option' do
|
15
|
+
expect do
|
16
|
+
Rack::ExceptionNotifier.new(good_app, {})
|
17
|
+
end.to raise_error(ArgumentError, 'to address is required')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
13
21
|
describe 'call' do
|
14
22
|
it 'does not send mail on success' do
|
15
23
|
notifier = Rack::ExceptionNotifier.new(good_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s')
|
@@ -27,7 +35,6 @@ describe Rack::ExceptionNotifier do
|
|
27
35
|
mail.to.should == ['bar@example.com']
|
28
36
|
mail.from.should == ['noreply@example.com']
|
29
37
|
mail.subject.should == 'testing - Test Message'
|
30
|
-
mail.body.raw_source.should_not include('Request Body')
|
31
38
|
end
|
32
39
|
|
33
40
|
it 'sends mail as user by default' do
|
@@ -40,13 +47,34 @@ describe Rack::ExceptionNotifier do
|
|
40
47
|
mail.from.should == [ENV['USER']]
|
41
48
|
end
|
42
49
|
|
43
|
-
it '
|
50
|
+
it 'does not include body by default' do
|
44
51
|
notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s')
|
45
52
|
expect do
|
46
53
|
notifier.call(env_with_body)
|
47
54
|
end.to raise_error(TestError)
|
48
55
|
|
49
56
|
mail = Mail::TestMailer.deliveries.first
|
57
|
+
mail.body.raw_source.should_not include('Request Body')
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'does not include body if not present in request' do
|
61
|
+
notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s', :include_body => true)
|
62
|
+
expect do
|
63
|
+
notifier.call(env)
|
64
|
+
end.to raise_error(TestError)
|
65
|
+
|
66
|
+
mail = Mail::TestMailer.deliveries.first
|
67
|
+
mail.body.raw_source.should_not include('Request Body')
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'includes the body if configured' do
|
71
|
+
notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s', :include_body => true)
|
72
|
+
expect do
|
73
|
+
notifier.call(env_with_body)
|
74
|
+
end.to raise_error(TestError)
|
75
|
+
|
76
|
+
mail = Mail::TestMailer.deliveries.first
|
77
|
+
mail.body.raw_source.should include('somethingspecial')
|
50
78
|
mail.body.raw_source.should include('somethingspecial')
|
51
79
|
end
|
52
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-exception_notifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Downey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mail
|