Permalink
Browse files

error handler reloading

  • Loading branch information...
1 parent a019a27 commit 12f49c589c5cf0e083adf9c8e5873ef1100109a4 @gnandretta gnandretta committed Jun 25, 2011
Showing with 58 additions and 0 deletions.
  1. +16 −0 lib/sinatra/reloader.rb
  2. +6 −0 spec/reloader/app.rb.erb
  3. +36 −0 spec/reloader_spec.rb
View
16 lib/sinatra/reloader.rb
@@ -208,6 +208,18 @@ def add_filter(type, path = nil, options = {}, &block)
result
end
+ # Does everything Sinatra::Base#error does, but it also tells
+ # the +Watcher::List+ for the Sinatra application to watch the
+ # defined error handler.
+ def error(*codes, &block)
+ path = caller_files[1] || File.expand_path($0)
+ result = super
+ codes.each do |c|
+ watch_element(path, :error, :code => c, :handler => @errors[c])
+ end
+ result
+ end
+
# Does everything Sinatra::Base#register does, but it also lets
# the reloader know that an extension is beign registered, because
# the elements defined in its +registered+ method need a special
@@ -244,6 +256,10 @@ def deactivate(element)
filters[:before].delete(element.representation)
when :after_filter then
filters[:after].delete(element.representation)
+ when :error then
+ code = element.representation[:code]
+ handler = element.representation[:handler]
+ @errors.delete(code) if @errors[code] == handler
end
end
View
6 spec/reloader/app.rb.erb
@@ -19,6 +19,12 @@ class <%= name %> < <%= parent %>
<%= filter %>
<% end %>
+<% errors.each do |number, code| %>
+ error <%= number %> do
+ <%= code %>
+ end
+<% end %>
+
<% routes.each do |route| %>
<%= route %>
<% end %>
View
36 spec/reloader_spec.rb
@@ -43,6 +43,7 @@ def write_app_file(options={})
options[:extensions] ||= []
options[:middlewares] ||= []
options[:filters] ||= []
+ options[:errors] ||= {}
options[:name] ||= app_name
options[:enable_reloader] = true unless options[:enable_reloader] === false
options[:parent] ||= 'Sinatra::Base'
@@ -221,6 +222,41 @@ def setup_example_app(options={})
end
end
+ describe "error reloading" do
+ before do
+ setup_example_app(
+ :routes => ['get("/secret") { 403 }'],
+ :errors => { 403 => "'Access forbiden'" }
+ )
+ end
+
+ it "doesn't mess up the application" do
+ get('/secret').should be_client_error
+ get('/secret').body.strip.should == 'Access forbiden'
+ end
+
+ it "knows when a error has been added" do
+ update_app_file(:errors => { 404 => "'Nowhere'" })
+ get('/nowhere').should be_not_found
+ get('/nowhere').body.should == 'Nowhere'
+ end
+
+ it "knows when a error has been removed" do
+ update_app_file(:routes => ['get("/secret") { 403 }'])
+ get('/secret').should be_client_error
+ get('/secret').body.should_not == 'Access forbiden'
+ end
+
+ it "knows when a error has been modified" do
+ update_app_file(
+ :routes => ['get("/secret") { 403 }'],
+ :errors => { 403 => "'What are you doing here?'" }
+ )
+ get('/secret').should be_client_error
+ get('/secret').body.should == 'What are you doing here?'
+ end
+ end
+
describe "extension reloading" do
it "doesn't duplicate routes with every reload" do
module ::RouteExtension

0 comments on commit 12f49c5

Please sign in to comment.