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:
parent
fbdf982f42
commit
1f07ba27b2
4 changed files with 18 additions and 13 deletions
2
Rakefile
2
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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -3,11 +3,7 @@ require File.dirname(__FILE__) + '/helper'
|
|||
context "Custom Errors (in general)" do
|
||||
|
||||
setup do
|
||||
Sinatra.application.options.raise_errors = false
|
||||
end
|
||||
|
||||
teardown do
|
||||
Sinatra.application.options.raise_errors = true
|
||||
Sinatra.application = nil
|
||||
end
|
||||
|
||||
specify "override the default 404" do
|
||||
|
@ -16,7 +12,7 @@ context "Custom Errors (in general)" do
|
|||
should.be.not_found
|
||||
body.should.equal '<h1>Not Found</h1>'
|
||||
|
||||
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 '<h1>Internal Server Error</h1>'
|
||||
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue