diff --git a/Rakefile b/Rakefile index 0011b2f4..d495bf80 100644 --- a/Rakefile +++ b/Rakefile @@ -5,5 +5,5 @@ task :default => :test Rake::TestTask.new do |t| ENV['SINATRA_ENV'] = 'test' - t.pattern = "test/*_test.rb" + t.pattern = File.dirname(__FILE__) + "/test/*_test.rb" end diff --git a/lib/sinatra.rb b/lib/sinatra.rb index e0919d9c..fd45ab88 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -28,6 +28,9 @@ end module Sinatra extend self + class NotFound < RuntimeError; end + class ServerError < RuntimeError; end + Result = Struct.new(:block, :params, :status) unless defined?(Result) def application @@ -483,7 +486,7 @@ module Sinatra method = env['REQUEST_METHOD'].downcase.to_sym e = static.invoke(env) e ||= events[method].eject(&[:invoke, env]) - e ||= (errors[404] || basic_not_found).invoke(env) + e ||= (errors[NotFound] || basic_not_found).invoke(env) e end @@ -533,7 +536,7 @@ module Sinatra raise e if options.raise_errors env['sinatra.error'] = e context.status(500) - result = (errors[e.class] || errors[500] || basic_error).invoke(env) + result = (errors[e.class] || errors[ServerError] || basic_error).invoke(env) returned = catch(:halt) do [:complete, context.instance_eval(&result.block)] end @@ -572,8 +575,8 @@ def helpers(&b) Sinatra::EventContext.class_eval(&b) end -def error(code, options = {}, &b) - Sinatra.application.define_error(code, options, &b) +def error(type = Sinatra::ServerError, options = {}, &b) + Sinatra.application.define_error(type, options, &b) end def layout(name = :layout, &b) diff --git a/test/custom_error_test.rb b/test/custom_error_test.rb index 0ba391a7..93e46d70 100644 --- a/test/custom_error_test.rb +++ b/test/custom_error_test.rb @@ -3,20 +3,16 @@ require File.dirname(__FILE__) + '/helper' context "Custom Errors (in general)" do setup do - Sinatra.application.options.raise_errors = false + Sinatra.application = nil end - teardown do - Sinatra.application.options.raise_errors = true - end - specify "override the default 404" do get_it '/' should.be.not_found body.should.equal '

Not Found

' - error 404 do + error Sinatra::NotFound do 'Custom 404' end @@ -27,6 +23,7 @@ context "Custom Errors (in general)" do end specify "override the default 500" do + Sinatra.application.options.raise_errors = false get '/' do raise 'asdf' @@ -37,7 +34,7 @@ context "Custom Errors (in general)" do body.should.equal '

Internal Server Error

' - error 500 do + error do 'Custom 500 for ' + request.env['sinatra.error'].message end @@ -47,6 +44,7 @@ context "Custom Errors (in general)" do status.should.equal 500 body.should.equal 'Custom 500 for asdf' + Sinatra.application.options.raise_errors = true end end diff --git a/test/mapped_error_test.rb b/test/mapped_error_test.rb index 09c24e0e..15c00ff6 100644 --- a/test/mapped_error_test.rb +++ b/test/mapped_error_test.rb @@ -4,11 +4,15 @@ class FooError < RuntimeError; end context "Mapped errors" do - before(:each) do + setup do Sinatra.application = nil Sinatra.application.options.raise_errors = false end + teardown do + Sinatra.application.options.raise_errors = true + end + specify "are rescued and run in context" do