Permalink
Browse files

Dump backtrace to rack.errors on unhandled exception

Also added a dump_errors option so that this can be disabled
when necessary.
  • Loading branch information...
1 parent 49c6015 commit b5faa571f51983e1a651b70c61185a553ae6d18c @rtomayko rtomayko committed Jan 9, 2009
Showing with 24 additions and 0 deletions.
  1. +4 −0 CHANGES
  2. +8 −0 lib/sinatra/base.rb
  3. +12 −0 test/mapped_error_test.rb
View
@@ -1,5 +1,9 @@
= 0.9.0 (unreleased)
+ * New "dump_errors" option controls whether the backtrace is dumped to
+ rack.errors when an exception is raised from a route. The option is
+ enabled by default for top-level apps.
+
* New ":provides" route condition takes an array of mime types and
matches only when an Accept request header is present with a
corresponding type. [cypher]
View
@@ -411,6 +411,12 @@ def error_detection
invoke handler unless handler.nil?
rescue ::Exception => boom
@env['sinatra.error'] = boom
+
+ if options.dump_errors?
+ msg = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n ")
+ @env['rack.errors'] << msg
+ end
+
raise boom if options.raise_errors?
@response.status = 500
invoke errmap[boom.class] || errmap[Exception]
@@ -660,6 +666,7 @@ def metadef(message, &block)
end
set :raise_errors, true
+ set :dump_errors, false
set :sessions, false
set :logging, false
set :methodoverride, false
@@ -753,6 +760,7 @@ def metadef(message, &block)
class Default < Base
set :raise_errors, false
+ set :dump_errors, true
set :sessions, false
set :logging, true
set :methodoverride, true
View
@@ -50,6 +50,18 @@ class FooError < RuntimeError
body.should.equal 'looks good'
end
+ it 'dumps errors to rack.errors when dump_errors is enabled' do
+ mock_app {
+ set :raise_errors, false
+ set :dump_errors, true
+ get('/') { raise FooError, 'BOOM!' }
+ }
+
+ get '/'
+ status.should.equal 500
+ @response.errors.should.match(/FooError - BOOM!:/)
+ end
+
it "raises without calling the handler when the raise_errors options is set" do
mock_app {
set :raise_errors, true

0 comments on commit b5faa57

Please sign in to comment.