low 0.0.16 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ require 'low'
2
+
3
+ module Low
4
+ module Rack
5
+
6
+ # +Low::Rack::Exceptions+ is like +Rack::ShowExceptions+, except it will
7
+ # only actually respond with the stack trace when give permission to. Also,
8
+ # it don't do HTML.
9
+ class Exceptions
10
+
11
+ def initialize(app, opts = {})
12
+ @app = app
13
+ @logger_key = opts[:logger_key] || Low::DEFAULT_LOGGER_KEY
14
+ end
15
+
16
+ def call(env)
17
+ @app.call(env)
18
+ rescue => exception
19
+
20
+ # Format the exception trace
21
+ trace = exception.message + "\n" + exception.backtrace.join("\n")
22
+
23
+ # and log it, if we have a logger, or raw IO.
24
+ if logger = env[@logger_key]
25
+ logger.fatal trace
26
+ elsif io = env['rack.errors']
27
+ io.puts(trace)
28
+ io.flush
29
+ end
30
+
31
+ # Respond with the trace if appropriate.
32
+ if env['low.show_exceptions']
33
+ message = trace
34
+ else
35
+ message = 'An internal server error occurred.'
36
+ end
37
+
38
+ [500, {'Content-Type' => 'text/plain'}, [message]]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,4 +1,5 @@
1
1
  require 'logger'
2
+ require 'low'
2
3
  require 'low/scoped_logger'
3
4
 
4
5
  module Low
@@ -11,7 +12,7 @@ module Low
11
12
 
12
13
  def initialize(app, opts = {})
13
14
  @app = app
14
- @key = opts[:key] || DEFAULT_KEY
15
+ @key = opts[:key] || Low::DEFAULT_LOGGER_KEY
15
16
  end
16
17
 
17
18
  def call(env)
data/lib/low/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Low
2
- VERSION = '0.0.16'
2
+ VERSION = '0.0.17'
3
3
  end
data/lib/low.rb CHANGED
@@ -3,4 +3,5 @@ $:.push File.dirname(__FILE__)
3
3
  require 'low/version'
4
4
 
5
5
  module Low
6
+ DEFAULT_LOGGER_KEY = 'rack.logger'
6
7
  end
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ require 'rack/mock'
4
+
5
+ require 'low/rack/exceptions'
6
+ require 'low/scoped_logger'
7
+
8
+ describe Low::Rack::Exceptions do
9
+
10
+ it 'should proxy successful calls transparently' do
11
+ base = lambda do |env|
12
+ [200, {'Content-Type' => 'text/plain'}, ['No Problems!']]
13
+ end
14
+ app = Low::Rack::Exceptions.new(base)
15
+
16
+ response = Rack::MockRequest.new(app).get('http://low.edu')
17
+ response.should be_ok
18
+ response.body.should == 'No Problems!'
19
+ end
20
+
21
+ it 'should log a stack trace to rack.logger when an error occurs' do
22
+ base = lambda { |env| raise 'gurp' }
23
+ app = Low::Rack::Exceptions.new(base)
24
+ io = StringIO.new
25
+
26
+ app.call('rack.logger' => Low::ScopedLogger.new(io))
27
+ io.rewind
28
+ lines = io.read.split("\n")
29
+ lines.length.should be > 5
30
+ lines.first.should =~ /gurp/
31
+ end
32
+
33
+ it 'should log a stack trace to rack.errors when an error occurs, and ' +
34
+ 'rack.logger is not specified' do
35
+ base = lambda { |env| raise 'gurp' }
36
+ app = Low::Rack::Exceptions.new(base)
37
+ io = StringIO.new
38
+
39
+ app.call('rack.errors' => io)
40
+ io.rewind
41
+ lines = io.read.split("\n")
42
+ lines.length.should be > 5
43
+ lines.first.should =~ /gurp/
44
+ end
45
+
46
+ it 'should respond with a 500 when an error occurs' do
47
+ base = lambda { |env| raise 'blurp' }
48
+ app = Low::Rack::Exceptions.new(base)
49
+
50
+ response = Rack::MockRequest.new(app).get('http://low.edu')
51
+ response.status.should == 500
52
+ end
53
+
54
+ it 'should include the trace in the response if low.show_exceptions is true' do
55
+ base = lambda { |env| raise 'glerb' }
56
+ app = Low::Rack::Exceptions.new(base)
57
+
58
+ response = app.call('low.show_exceptions' => true)
59
+ lines = response[2].first.split("\n")
60
+ lines.length.should be > 5
61
+ lines.first.should =~ /glerb/
62
+ end
63
+
64
+ it 'should show a generic message if low.show_exceptions is falsey' do
65
+ base = lambda { |env| raise 'glorp' }
66
+ app = Low::Rack::Exceptions.new(base)
67
+
68
+ response = app.call({})
69
+ response[2].first.should == 'An internal server error occurred.'
70
+ end
71
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: low
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-21 00:00:00.000000000 Z
12
+ date: 2013-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -109,6 +109,7 @@ files:
109
109
  - lib/low/mongo.rb
110
110
  - lib/low/mongo/heroku.rb
111
111
  - lib/low/mongo/util.rb
112
+ - lib/low/rack/exceptions.rb
112
113
  - lib/low/rack/log_level.rb
113
114
  - lib/low/rack/rack_errors.rb
114
115
  - lib/low/rack/request_id.rb
@@ -119,6 +120,7 @@ files:
119
120
  - low.gemspec
120
121
  - spec/low/mongo/util_spec.rb
121
122
  - spec/low/mongo_spec.rb
123
+ - spec/low/rack/exceptions_spec.rb
122
124
  - spec/low/rack/log_level_spec.rb
123
125
  - spec/low/rack/rack_errors_spec.rb
124
126
  - spec/low/rack/request_id_spec.rb
@@ -153,6 +155,7 @@ summary: A low-level utility library.
153
155
  test_files:
154
156
  - spec/low/mongo/util_spec.rb
155
157
  - spec/low/mongo_spec.rb
158
+ - spec/low/rack/exceptions_spec.rb
156
159
  - spec/low/rack/log_level_spec.rb
157
160
  - spec/low/rack/rack_errors_spec.rb
158
161
  - spec/low/rack/request_id_spec.rb