Skip to content

Commit

Permalink
support handle exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
migrs committed Apr 17, 2012
1 parent 8c2c1d2 commit d4dd6c2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
11 changes: 9 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ CHANGES

* [Changes](https://github.com/migrs/rack-server-pages/compare/v0.0.4...master)

* Feature
- Handle exceptions
- Add `config.show_exceptions` option
- Invoke `on_error` filter when an exception is caught

* Enhancement
- NotFound
- NotFound with 404 file
- Rack::ServerPages::NotFound['404.html']
- On Error

* Bugfix
- Duplicated filters

### [0.0.4](https://github.com/migrs/rack-server-pages/tree/v0.0.4) / 2012-01-15

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ There are no controllers or models, just only views like a jsp, asp and php!
- Include a partial template
- Layout template
- Before/After filters
- Handle exceptions
- Include helpers
- Integrate with any rack applications (Rails, Sinatra, etc...)
- Extremely simple and easy to use!
Expand Down
6 changes: 6 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module SampleHelper
end

run Rack::ServerPages.new { |config|
#config.show_exceptions = false

config.on_error do
response.body = ['Error!']
end

config.helpers SampleHelper
config.helpers do
def sample4
Expand Down
43 changes: 32 additions & 11 deletions lib/rack/server_pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,22 @@ def build_response(template, scope)
def server_page(template)
lambda do |env|
@binding.new(env).tap do |scope|
catch(:halt) { @config.filter.invoke(scope) { build_response(template, scope) }}
catch(:halt) do
begin
@config.filter.invoke(scope, :before)
build_response(template, scope)
rescue
if @config.show_exceptions?
raise $!
else
scope.response.status = 500
scope.response['Content-Type'] ||= "text/html"
@config.filter.invoke(scope, :on_error)
end
ensure
@config.filter.invoke(scope, :after)
end
end
end.response.finish
end
end
Expand All @@ -91,24 +106,20 @@ def [] type
end

def merge(other)
TYPES.each { |type| self[type].concat other[type] }
TYPES.each { |type| @filters[type].concat other[type] }
end

def merge_from_helpers(helpers)
merge(self.class.extract_filters_from_helpers(helpers))
end

def add(type, *args, &block)
self[type] << block if block_given?
self[type].concat args unless args.empty?
@filters[type] << block if block_given?
@filters[type].concat args unless args.empty?
end

def invoke(scope, type = nil)
if block_given?
invoke(scope, TYPES.first); yield scope; invoke(scope, TYPES.last)
else
self[type].each { |f| f.respond_to?(:bind) ? f.bind(scope).call : scope.instance_exec(&f) }
end
def invoke(scope, type)
@filters[type].each { |f| f.respond_to?(:bind) ? f.bind(scope).call : scope.instance_exec(&f) }
end

def self.extract_filters_from_helpers(helpers)
Expand Down Expand Up @@ -136,8 +147,10 @@ def self.hash_accessor(*names)
end
end

hash_accessor :view_path, :effective_path, :cache_control, :default_charset, :failure_app
hash_accessor :view_path, :effective_path, :cache_control, :default_charset, :failure_app, :show_exceptions

attr_reader :filter

def_delegators :filter, *Filter::TYPES

def initialize
Expand All @@ -148,6 +161,14 @@ def initialize
@filter = Filter.new
end

def show_exceptions?
if self[:show_exceptions].nil?
ENV['RACK_ENV'].nil? or (ENV['RACK_ENV'] == 'development')
else
self[:show_exceptions]
end
end

def view_paths
(v = self[:view_path]).kind_of?(Enumerable) ? v : [v.to_s]
end
Expand Down

0 comments on commit d4dd6c2

Please sign in to comment.