Catch exceptions at Rack level. closes #183

This commit is contained in:
Thomas Reynolds 2011-12-09 11:11:17 -08:00
parent 1feea222c0
commit c48679e993
3 changed files with 48 additions and 10 deletions

View file

@ -64,6 +64,9 @@ module Middleman
# Pass custom options to views # Pass custom options to views
autoload :Routing, "middleman/core_extensions/routing" autoload :Routing, "middleman/core_extensions/routing"
# Catch and show exceptions at the Rack level
autoload :ShowExceptions, "middleman/core_extensions/show_exceptions"
end end
module Extensions module Extensions

View file

@ -59,7 +59,17 @@ class Middleman::Base
# @return [Rack::Builder] # @return [Rack::Builder]
def to_rack_app(&block) def to_rack_app(&block)
inner_app = inst(&block) inner_app = inst(&block)
(@middleware || []).each do |m|
app.use(m[0], *m[1], &m[2])
end
app.map("/") { run inner_app } app.map("/") { run inner_app }
(@mappings || []).each do |m|
app.map(m[0], &m[1])
end
app app
end end
@ -82,14 +92,16 @@ class Middleman::Base
# #
# @param [Class] Middleware # @param [Class] Middleware
def use(middleware, *args, &block) def use(middleware, *args, &block)
app.use(middleware, *args, &block) @middleware ||= []
@middleware << [middleware, args, block]
end end
# Add Rack App mapped to specific path # Add Rack App mapped to specific path
# #
# @param [String] Path to map # @param [String] Path to map
def map(map, &block) def map(map, &block)
app.map(map, &block) @mappings ||= []
@mappings << [map, block]
end end
# Mix-in helper methods. Accepts either a list of Modules # Mix-in helper methods. Accepts either a list of Modules
@ -168,6 +180,10 @@ class Middleman::Base
# @return [String] # @return [String]
set :http_prefix, "/" set :http_prefix, "/"
# Whether to catch and display exceptions
# @return [Boolean]
set :show_exceptions, true
# Automatically loaded extensions # Automatically loaded extensions
# @return [Array<Symbol>] # @return [Array<Symbol>]
set :default_extensions, [ set :default_extensions, [
@ -182,6 +198,9 @@ class Middleman::Base
# Activate custom features and extensions # Activate custom features and extensions
include Middleman::CoreExtensions::Extensions include Middleman::CoreExtensions::Extensions
# Handle exceptions
register Middleman::CoreExtensions::ShowExceptions
# Add Builder Callbacks # Add Builder Callbacks
register Middleman::CoreExtensions::Builder register Middleman::CoreExtensions::Builder

View file

@ -0,0 +1,16 @@
require 'rack/showexceptions'
module Middleman::CoreExtensions::ShowExceptions
class << self
def registered(app)
app.after_configuration do
if show_exceptions
use ::Middleman::CoreExtensions::ShowExceptions::Middleware
end
end
end
end
class Middleware < ::Rack::ShowExceptions
end
end