i can has error map?

This commit is contained in:
Blake Mizerany 2008-02-23 17:00:04 -08:00
parent 776d39aa0d
commit ba2d234cce
2 changed files with 67 additions and 3 deletions

View File

@ -360,13 +360,14 @@ module Sinatra
rescue => e
raise e if options.raise_errors
env['sinatra.error'] = e
result = (errors[500] || basic_error).invoke(env)
context.status(500)
result = (errors[e.class] || errors[500] || basic_error).invoke(env)
returned = catch(:halt) do
[:complete, context.instance_eval(&result.block)]
end
body = returned.to_result(context)
context.status(500)
context.body = String === body ? [*body] : body
body = '' unless body.respond_to?(:each)
context.body = body.kind_of?(String) ? [*body] : body
context.finish
end
end

63
test/mapped_error_test.rb Normal file
View File

@ -0,0 +1,63 @@
require File.dirname(__FILE__) + '/helper'
class FooError < RuntimeError; end
context "Mapped errors" do
before(:each) do
Sinatra.application = nil
Sinatra.application.options.raise_errors = false
end
specify "are rescued and run in context" do
error FooError do
'MAPPED ERROR!'
end
get '/' do
raise FooError.new
end
get_it '/'
should.be.server_error
body.should.equal 'MAPPED ERROR!'
end
specify "renders empty if no each method on result" do
error FooError do
nil
end
get '/' do
raise FooError.new
end
get_it '/'
should.be.server_error
body.should.be.empty
end
specify "doesn't override status if set" do
error FooError do
status(200)
end
get '/' do
raise FooError.new
end
get_it '/'
should.be.ok
end
end