Skip to content

Commit

Permalink
styled_halt
Browse files Browse the repository at this point in the history
  • Loading branch information
slivu committed Apr 27, 2013
1 parent c0883ed commit 5eeb404
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
1 change: 0 additions & 1 deletion lib/e-core.rb
Expand Up @@ -18,7 +18,6 @@
require 'e-core/instance/base'
require 'e-core/instance/cookies'
require 'e-core/instance/halt'
require 'e-core/instance/error_handler'
require 'e-core/instance/invoke'
require 'e-core/instance/redirect'
require 'e-core/instance/request'
Expand Down
8 changes: 4 additions & 4 deletions lib/e-core/instance/base.rb
Expand Up @@ -33,12 +33,12 @@ def setup_action! action = nil
if setup = self.class.action_setup[action]
self.action_setup = setup[env[EConstants::ENV__REQUEST_METHOD]] || setup[:*]
self.action_setup ||
fail(EConstants::STATUS__NOT_IMPLEMENTED, "Resource found
styled_halt(EConstants::STATUS__NOT_IMPLEMENTED, "Resource found
but it can be accessed only through %s" % setup.keys.join(", "))
end
end
self.action_setup ||
fail(EConstants::STATUS__NOT_FOUND, '%s %s not found' % [rq.request_method, rq.path])
styled_halt(EConstants::STATUS__NOT_FOUND, '%s %s not found' % [rq.request_method, rq.path])
end
private :setup_action!

Expand Down Expand Up @@ -73,10 +73,10 @@ def call env
given = action_params__array.size

min && given < min &&
fail(EConstants::STATUS__NOT_FOUND, 'min params accepted: %s; params given: %s' % [min, given])
styled_halt(EConstants::STATUS__NOT_FOUND, 'min params accepted: %s; params given: %s' % [min, given])

max && given > max &&
fail(EConstants::STATUS__NOT_FOUND, 'max params accepted: %s; params given: %s' % [max, given])
styled_halt(EConstants::STATUS__NOT_FOUND, 'max params accepted: %s; params given: %s' % [max, given])

call!
end
Expand Down
40 changes: 0 additions & 40 deletions lib/e-core/instance/error_handler.rb

This file was deleted.

42 changes: 42 additions & 0 deletions lib/e-core/instance/halt.rb
Expand Up @@ -24,6 +24,7 @@ class E
# halt [200, {'Content-Disposition' => "attachment; filename=some-file"}, some_IO_instance]
#
# @param [Array] *args
#
def halt *args
args.each do |a|
case a
Expand All @@ -44,4 +45,45 @@ def halt *args
throw :__e__catch__response__, response
end

# same as `halt` except it carrying earlier defined error handlers.
# if no handler found it behaves exactly as `halt(error_code[, body])`.
#
# @example
# class App < E
#
# # defining the proc to be executed on 404 errors
# error 404 do |message|
# render_layout('layouts/404') { message }
# end
#
# def index id, status
# item = Model.fisrt(:id => id, :status => status)
# unless item
# # interrupt execution and send a styled 404 error to browser.
# styled_halt 404, 'Can not find item by given ID and Status'
# end
# # code to be executed only if item found
# end
# end
#
def styled_halt error_code = EConstants::STATUS__SERVER_ERROR, body = nil
if handler = error_handler_defined?(error_code)
meth, arity = handler
body = arity > 0 ? self.send(meth, body) : [self.send(meth), body].join
end
halt error_code.to_i, body
end
alias styled_error styled_halt
alias styled_error! styled_halt
alias fail styled_halt
alias fail! styled_halt
alias quit styled_halt
alias quit! styled_halt
alias error styled_halt
alias error! styled_halt

def error_handler_defined? error_code
self.class.error_handler(error_code) || self.class.error_handler(:*)
end

end

0 comments on commit 5eeb404

Please sign in to comment.