Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: sinatra/sinatra-contrib
...
head fork: sinatra/sinatra-contrib
Checking mergeability… Don’t worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
Commits on Mar 25, 2012
@TrevorBramble TrevorBramble Merge remote-tracking branch 'origin/master' into 1.3.x 08093aa
Commits on Mar 28, 2012
@unakatsuo unakatsuo Fix the issue that ignores error handlers in base. 37d28e5
Commits on Mar 29, 2012
@unakatsuo unakatsuo simpler way to look up error handlers in base.
Eventually it is just needed to merge @errors in both the namaspace module
and the base class. Sinatra::Base#error_block!() is enough functional to
chase up merged @errors as of v1.3.0.
d14b588
Commits on Apr 10, 2012
@TrevorBramble TrevorBramble Merge branch 'namespace-error-fix' of github.com:unakatsuo/sinatra-co…
…ntrib into 1.3.x
3029a01
View
58 Gemfile.lock
@@ -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
29 lib/sinatra/namespace.rb
@@ -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
4 sinatra-contrib.gemspec
@@ -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
42 spec/namespace_spec.rb
@@ -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
View
8 spec/reloader_spec.rb
@@ -11,14 +11,14 @@ def tmp_dir
# Returns the path of the Sinatra application file created by
# +setup_example_app+.
def app_file_path
- File.join(tmp_dir, "example_app_#{@@example_app_counter}.rb")
+ File.join(tmp_dir, "example_app_#{$example_app_counter}.rb")
end
# Returns the name of the Sinatra application created by
# +setup_example_app+: 'ExampleApp1' for the first application,
# 'ExampleApp2' fo the second one, and so on...
def app_name
- "ExampleApp#{@@example_app_counter}"
+ "ExampleApp#{$example_app_counter}"
end
# Returns the (constant of the) Sinatra application created by
@@ -70,8 +70,8 @@ def update_file(path, &block)
# the new application as the one being tested and enables the
# reloader.
def setup_example_app(options={})
- @@example_app_counter ||= 0
- @@example_app_counter += 1
+ $example_app_counter ||= 0
+ $example_app_counter += 1
FileUtils.mkdir_p(tmp_dir)
write_app_file(options)

No commit comments for this range

Something went wrong with that request. Please try again.