Permalink
Browse files

Merge branch 'namespace-error-fix' of github.com:unakatsuo/sinatra-co…

…ntrib into 1.3.x
  • Loading branch information...
TrevorBramble committed Apr 10, 2012
2 parents 08093aa + d14b588 commit 3029a01f8aed36a72f88633f46e3b3bf42e8bf6b
Showing with 85 additions and 48 deletions.
  1. +27 −31 Gemfile.lock
  2. +14 −15 lib/sinatra/namespace.rb
  3. +2 −2 sinatra-contrib.gemspec
  4. +42 −0 spec/namespace_spec.rb
View
@@ -1,58 +1,54 @@
GIT
remote: git://github.com/sinatra/sinatra
- revision: 962d3f549ec6b7a2adbbee8438f03b28cdf80d6f
+ revision: b882ab37df7d0a850d6e2091507dbc66d1ae2ea0
specs:
- sinatra (1.3.0)
- rack (~> 1.3)
- tilt (~> 1.3)
+ sinatra (1.4.0)
+ rack (~> 1.3, >= 1.3.6)
+ rack-protection (~> 1.2)
+ tilt (~> 1.3, >= 1.3.3)
PATH
remote: .
specs:
- sinatra-contrib (1.3.0)
+ sinatra-contrib (1.4.0)
backports (>= 2.0)
eventmachine
rack-protection
rack-test
- sinatra (~> 1.3.0)
+ sinatra (~> 1.4.0)
tilt (~> 1.3)
GEM
remote: http://rubygems.org/
specs:
backports (2.3.0)
- diff-lcs (1.1.2)
+ diff-lcs (1.1.3)
erubis (2.7.0)
- escape_utils (0.2.3)
eventmachine (0.12.10)
- eventmachine (0.12.10-java)
- haml (3.1.2)
- json (1.5.1)
- json (1.5.1-java)
- rack (1.3.2)
- rack-protection (0.1.0)
- escape_utils
+ haml (3.1.4)
+ json (1.6.5)
+ rack (1.4.1)
+ rack-protection (1.2.0)
rack
rack-test (0.6.1)
rack (>= 1.0)
- rake (0.9.2)
- rspec (2.6.0)
- rspec-core (~> 2.6.0)
- rspec-expectations (~> 2.6.0)
- rspec-mocks (~> 2.6.0)
- rspec-core (2.6.4)
- rspec-expectations (2.6.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.6.0)
- slim (0.9.4)
- temple (~> 0.3.0)
- tilt (~> 1.2)
- temple (0.3.1)
- tilt (1.3.2)
- yajl-ruby (0.8.3)
+ rake (0.9.2.2)
+ rspec (2.9.0)
+ rspec-core (~> 2.9.0)
+ rspec-expectations (~> 2.9.0)
+ rspec-mocks (~> 2.9.0)
+ rspec-core (2.9.0)
+ rspec-expectations (2.9.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.9.0)
+ slim (1.1.1)
+ temple (~> 0.4.0)
+ tilt (~> 1.3.2)
+ temple (0.4.0)
+ tilt (1.3.3)
+ yajl-ruby (1.1.0)
PLATFORMS
- java
ruby
DEPENDENCIES
View
@@ -118,7 +118,7 @@ def self.new(base, pattern, conditions = {}, &block)
Module.new do
extend NamespacedMethods
include InstanceMethods
- @base, @extensions = base, []
+ @base, @extensions, @errors = base, [], {}
@pattern, @conditions = compile(pattern, conditions)
@templates = Hash.new { |h,k| @base.templates[k] }
namespace = self
@@ -135,14 +135,6 @@ def settings
def template_cache
super.fetch(:nested, @namespace) { Tilt::Cache.new }
end
-
- def error_block!(*keys)
- if block = keys.inject(nil) { |b,k| b ||= @namespace.errors[k] }
- instance_eval(&block)
- else
- super
- end
- end
end
module SharedMethods
@@ -180,16 +172,23 @@ def invoke_hook(name, *args)
@extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
end
- def errors
- @errors ||= {}
- end
-
def not_found(&block)
error(404, &block)
end
- def error(codes = Exception, &block)
- [*codes].each { |c| errors[c] = block }
+ def errors
+ base.errors.merge(@errors)
+ end
+
+ def namespace_errors
+ @errors
+ end
+
+ def error(*codes, &block)
+ args = Sinatra::Base.send(:compile!, "ERROR", /^#{@pattern}/, block)
+ codes = codes.map { |c| Array(c) }.flatten
+ codes << Exception if codes.empty?
+ codes.each { |c| @errors[c] = args }
end
def respond_to(*args)
View
@@ -1,7 +1,7 @@
# Run `rake sinatra-contrib.gemspec` to update the gemspec.
Gem::Specification.new do |s|
s.name = "sinatra-contrib"
- s.version = "1.3.2"
+ s.version = "1.4.0"
s.description = "Collection of useful Sinatra extensions"
s.homepage = "http://github.com/sinatra/sinatra-contrib"
s.summary = s.description
@@ -111,7 +111,7 @@ Gem::Specification.new do |s|
"spec/streaming_spec.rb"
]
- s.add_dependency "sinatra", "~> 1.3.0"
+ s.add_dependency "sinatra", "~> 1.4.0"
s.add_dependency "backports", ">= 2.0"
s.add_dependency "tilt", "~> 1.3"
s.add_dependency "rack-test"
View
@@ -487,6 +487,48 @@ def magic
get('/de/foo').status.should == 404
last_response.body.should == 'nicht gefunden' unless verb == :head
end
+
+ it "should handle custom errors in base. Issue #37." do
+ mock_app {
+ error(404) { 'not found...' }
+ namespace('/en') do
+ end
+ namespace('/de') do
+ error(404) { 'nicht gefunden' }
+ end
+ }
+ send(verb, '/foo').status.should == 404
+ last_response.body.should == 'not found...' unless verb == :head
+ get('/en/foo').status.should == 404
+ last_response.body.should == 'not found...' unless verb == :head
+ get('/de/foo').status.should == 404
+ last_response.body.should == 'nicht gefunden' unless verb == :head
+ end
+
+ it "should allow custom error handlers with Exception class. Issue #37." do
+ mock_app {
+ class AError < StandardError; end
+ class BError < AError; end
+
+ error(AError) { body('auth failed'); 401}
+ namespace('/en') do
+ get '/foo' do
+ raise BError
+ end
+ end
+ namespace('/de') do
+ error(AError) { body('methode nicht erlaubt'); 406}
+
+ get '/foo' do
+ raise BError
+ end
+ end
+ }
+ get('/en/foo').status.should == 401
+ last_response.body.should == 'auth failed' unless verb == :head
+ get('/de/foo').status.should == 406
+ last_response.body.should == 'methode nicht erlaubt' unless verb == :head
+ end
end
describe 'templates' do

0 comments on commit 3029a01

Please sign in to comment.