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

@ -5,12 +5,12 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
# Top-level Middleman object # Top-level Middleman object
module Middleman module Middleman
# Auto-load modules on-demand # Auto-load modules on-demand
autoload :Base, "middleman/base" autoload :Base, "middleman/base"
autoload :Cache, "middleman/cache" autoload :Cache, "middleman/cache"
autoload :Builder, "middleman/builder" autoload :Builder, "middleman/builder"
autoload :CLI, "middleman/cli" autoload :CLI, "middleman/cli"
autoload :Templates, "middleman/templates" autoload :Templates, "middleman/templates"
autoload :Guard, "middleman/guard" autoload :Guard, "middleman/guard"
# Custom Renderers # Custom Renderers
module Renderers module Renderers
@ -57,13 +57,16 @@ module Middleman
autoload :Rendering, "middleman/core_extensions/rendering" autoload :Rendering, "middleman/core_extensions/rendering"
# Compass framework for Sass # Compass framework for Sass
autoload :Compass, "middleman/core_extensions/compass" autoload :Compass, "middleman/core_extensions/compass"
# Sprockets 2 # Sprockets 2
autoload :Sprockets, "middleman/core_extensions/sprockets" autoload :Sprockets, "middleman/core_extensions/sprockets"
# 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, [
@ -181,6 +197,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