mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Handle multiple errors with one block
This commit is contained in:
parent
7094ecc8c0
commit
7047de98a1
6 changed files with 37 additions and 19 deletions
|
@ -88,13 +88,21 @@ module Sinatra
|
|||
end
|
||||
|
||||
def config
|
||||
@config ||= {}
|
||||
@config ||= @default_config
|
||||
end
|
||||
|
||||
def config=(c)
|
||||
@config = c
|
||||
end
|
||||
|
||||
def default_config
|
||||
@default_config ||= {
|
||||
:run => true,
|
||||
:raise_errors => false,
|
||||
:env => :development
|
||||
}
|
||||
end
|
||||
|
||||
def determine_route(verb, path)
|
||||
routes[verb].eject { |r| r.match(path) } || routes[404]
|
||||
end
|
||||
|
@ -177,9 +185,9 @@ def get(path, &b)
|
|||
Sinatra.define_route(:get, path, &b)
|
||||
end
|
||||
|
||||
def error(code, &b)
|
||||
def error(*codes, &b)
|
||||
raise 'You must specify a block to assciate with an error' if b.nil?
|
||||
Sinatra.define_error(code, &b)
|
||||
codes.each { |code| Sinatra.define_error(code, &b) }
|
||||
end
|
||||
|
||||
Sinatra.setup_default_events!
|
||||
|
|
|
@ -4,3 +4,7 @@ require File.dirname(__FILE__) + "/test/methods"
|
|||
class Test::Unit::TestCase
|
||||
include Sinatra::Test::Methods
|
||||
end
|
||||
|
||||
include Sinatra::Test::Methods
|
||||
|
||||
Sinatra.default_config[:raise_errors] = true
|
||||
|
|
|
@ -28,10 +28,16 @@ module Sinatra
|
|||
get_it(@response.location)
|
||||
end
|
||||
|
||||
def dont_raise_errors
|
||||
Sinatra.config[:raise_errors] = false
|
||||
yield
|
||||
Sinatra.config[:raise_errors] = true
|
||||
end
|
||||
|
||||
def method_missing(name, *args)
|
||||
@response.send(name, *args)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,16 +1,9 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
context "Dispatching" do
|
||||
|
||||
include Sinatra::Test::Methods
|
||||
|
||||
def dont_raise_errors
|
||||
Sinatra.config[:raise_errors] = false
|
||||
yield
|
||||
Sinatra.config[:raise_errors] = true
|
||||
end
|
||||
|
||||
|
||||
setup do
|
||||
Sinatra.config = nil
|
||||
Sinatra.routes.clear
|
||||
Sinatra.setup_default_events!
|
||||
end
|
||||
|
@ -102,8 +95,6 @@ end
|
|||
|
||||
context "An Event in test mode" do
|
||||
|
||||
include Sinatra::Test::Methods
|
||||
|
||||
setup do
|
||||
Sinatra.routes.clear
|
||||
Sinatra.setup_default_events!
|
||||
|
|
|
@ -4,6 +4,7 @@ context "Defining Errors" do
|
|||
|
||||
setup do
|
||||
Sinatra.routes.clear
|
||||
Sinatra.config = nil
|
||||
Sinatra.setup_default_events!
|
||||
end
|
||||
|
||||
|
@ -24,9 +25,9 @@ context "Defining Errors" do
|
|||
should.be.not_found
|
||||
end
|
||||
|
||||
xspecify "should handle multiple errors" do
|
||||
specify "should handle multiple errors" do
|
||||
|
||||
get 404, 500 do
|
||||
error 404, 500 do
|
||||
'multi custom error'
|
||||
end
|
||||
|
||||
|
@ -34,7 +35,17 @@ context "Defining Errors" do
|
|||
raise 'asdf'
|
||||
end
|
||||
|
||||
dont_raise_errors do
|
||||
get_it '/error'
|
||||
end
|
||||
|
||||
status.should.equal 500
|
||||
body.should.equal 'multi custom error'
|
||||
|
||||
get_it '/'
|
||||
status.should.equal 404
|
||||
|
||||
body.should.equal 'multi custom error'
|
||||
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
require File.dirname(__FILE__) + '/../lib/sinatra'
|
||||
require File.dirname(__FILE__) + '/../lib/sinatra/test/spec'
|
||||
require 'mocha'
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue