1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

No more Error codes. Map them by Class name

This commit is contained in:
Blake Mizerany 2008-02-24 16:43:39 -08:00
parent fbdf982f42
commit 1f07ba27b2
4 changed files with 18 additions and 13 deletions

View file

@ -5,5 +5,5 @@ task :default => :test
Rake::TestTask.new do |t| Rake::TestTask.new do |t|
ENV['SINATRA_ENV'] = 'test' ENV['SINATRA_ENV'] = 'test'
t.pattern = "test/*_test.rb" t.pattern = File.dirname(__FILE__) + "/test/*_test.rb"
end end

View file

@ -28,6 +28,9 @@ end
module Sinatra module Sinatra
extend self extend self
class NotFound < RuntimeError; end
class ServerError < RuntimeError; end
Result = Struct.new(:block, :params, :status) unless defined?(Result) Result = Struct.new(:block, :params, :status) unless defined?(Result)
def application def application
@ -483,7 +486,7 @@ module Sinatra
method = env['REQUEST_METHOD'].downcase.to_sym method = env['REQUEST_METHOD'].downcase.to_sym
e = static.invoke(env) e = static.invoke(env)
e ||= events[method].eject(&[: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 e
end end
@ -533,7 +536,7 @@ module Sinatra
raise e if options.raise_errors raise e if options.raise_errors
env['sinatra.error'] = e env['sinatra.error'] = e
context.status(500) 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 returned = catch(:halt) do
[:complete, context.instance_eval(&result.block)] [:complete, context.instance_eval(&result.block)]
end end
@ -572,8 +575,8 @@ def helpers(&b)
Sinatra::EventContext.class_eval(&b) Sinatra::EventContext.class_eval(&b)
end end
def error(code, options = {}, &b) def error(type = Sinatra::ServerError, options = {}, &b)
Sinatra.application.define_error(code, options, &b) Sinatra.application.define_error(type, options, &b)
end end
def layout(name = :layout, &b) def layout(name = :layout, &b)

View file

@ -3,11 +3,7 @@ require File.dirname(__FILE__) + '/helper'
context "Custom Errors (in general)" do context "Custom Errors (in general)" do
setup do setup do
Sinatra.application.options.raise_errors = false Sinatra.application = nil
end
teardown do
Sinatra.application.options.raise_errors = true
end end
specify "override the default 404" do specify "override the default 404" do
@ -16,7 +12,7 @@ context "Custom Errors (in general)" do
should.be.not_found should.be.not_found
body.should.equal '<h1>Not Found</h1>' body.should.equal '<h1>Not Found</h1>'
error 404 do error Sinatra::NotFound do
'Custom 404' 'Custom 404'
end end
@ -27,6 +23,7 @@ context "Custom Errors (in general)" do
end end
specify "override the default 500" do specify "override the default 500" do
Sinatra.application.options.raise_errors = false
get '/' do get '/' do
raise 'asdf' raise 'asdf'
@ -37,7 +34,7 @@ context "Custom Errors (in general)" do
body.should.equal '<h1>Internal Server Error</h1>' body.should.equal '<h1>Internal Server Error</h1>'
error 500 do error do
'Custom 500 for ' + request.env['sinatra.error'].message 'Custom 500 for ' + request.env['sinatra.error'].message
end end
@ -47,6 +44,7 @@ context "Custom Errors (in general)" do
status.should.equal 500 status.should.equal 500
body.should.equal 'Custom 500 for asdf' body.should.equal 'Custom 500 for asdf'
Sinatra.application.options.raise_errors = true
end end
end end

View file

@ -4,11 +4,15 @@ class FooError < RuntimeError; end
context "Mapped errors" do context "Mapped errors" do
before(:each) do setup do
Sinatra.application = nil Sinatra.application = nil
Sinatra.application.options.raise_errors = false Sinatra.application.options.raise_errors = false
end end
teardown do
Sinatra.application.options.raise_errors = true
end
specify "are rescued and run in context" do specify "are rescued and run in context" do