error handler reloading

This commit is contained in:
Gabriel Andretta 2011-06-25 20:39:26 -03:00
parent bb59fe6b93
commit 5a0d33abd3
3 changed files with 58 additions and 0 deletions

View File

@ -208,6 +208,18 @@ module Sinatra
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 @@ module Sinatra
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 File

@ -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 File

@ -43,6 +43,7 @@ describe Sinatra::Reloader do
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 @@ describe Sinatra::Reloader do
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