diff --git a/lib/middleman.rb b/lib/middleman.rb index 71dc102a..aeb4da78 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -5,12 +5,12 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) # Top-level Middleman object module Middleman # Auto-load modules on-demand - autoload :Base, "middleman/base" - autoload :Cache, "middleman/cache" - autoload :Builder, "middleman/builder" - autoload :CLI, "middleman/cli" - autoload :Templates, "middleman/templates" - autoload :Guard, "middleman/guard" + autoload :Base, "middleman/base" + autoload :Cache, "middleman/cache" + autoload :Builder, "middleman/builder" + autoload :CLI, "middleman/cli" + autoload :Templates, "middleman/templates" + autoload :Guard, "middleman/guard" # Custom Renderers module Renderers @@ -57,13 +57,16 @@ module Middleman autoload :Rendering, "middleman/core_extensions/rendering" # Compass framework for Sass - autoload :Compass, "middleman/core_extensions/compass" + autoload :Compass, "middleman/core_extensions/compass" # Sprockets 2 - autoload :Sprockets, "middleman/core_extensions/sprockets" + autoload :Sprockets, "middleman/core_extensions/sprockets" # Pass custom options to views autoload :Routing, "middleman/core_extensions/routing" + + # Catch and show exceptions at the Rack level + autoload :ShowExceptions, "middleman/core_extensions/show_exceptions" end module Extensions diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 8393c8db..27ca2c88 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -59,7 +59,17 @@ class Middleman::Base # @return [Rack::Builder] def to_rack_app(&block) inner_app = inst(&block) + + (@middleware || []).each do |m| + app.use(m[0], *m[1], &m[2]) + end + app.map("/") { run inner_app } + + (@mappings || []).each do |m| + app.map(m[0], &m[1]) + end + app end @@ -82,14 +92,16 @@ class Middleman::Base # # @param [Class] Middleware def use(middleware, *args, &block) - app.use(middleware, *args, &block) + @middleware ||= [] + @middleware << [middleware, args, block] end # Add Rack App mapped to specific path # # @param [String] Path to map def map(map, &block) - app.map(map, &block) + @mappings ||= [] + @mappings << [map, block] end # Mix-in helper methods. Accepts either a list of Modules @@ -168,6 +180,10 @@ class Middleman::Base # @return [String] set :http_prefix, "/" + # Whether to catch and display exceptions + # @return [Boolean] + set :show_exceptions, true + # Automatically loaded extensions # @return [Array] set :default_extensions, [ @@ -181,6 +197,9 @@ class Middleman::Base # Activate custom features and extensions include Middleman::CoreExtensions::Extensions + + # Handle exceptions + register Middleman::CoreExtensions::ShowExceptions # Add Builder Callbacks register Middleman::CoreExtensions::Builder diff --git a/lib/middleman/core_extensions/show_exceptions.rb b/lib/middleman/core_extensions/show_exceptions.rb new file mode 100644 index 00000000..ad21b0f5 --- /dev/null +++ b/lib/middleman/core_extensions/show_exceptions.rb @@ -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