Skip to content

Commit

Permalink
Merge branch 'weppos-halt-with-message'
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jan 19, 2015
2 parents 815aee9 + 141dd97 commit c9475e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/lotus/action/throwable.rb
Expand Up @@ -62,24 +62,29 @@ def handle_exception(exception)

protected

# Halt the action execution with the given HTTP status code.
# Halt the action execution with the given HTTP status code and message.
#
# When used, the execution of a callback or of an action is interrupted
# and the control returns to the framework, that decides how to handle
# the event.
#
# It also sets the response body with the message associated to the code
# If a message is provided, it sets the response body with the message.
# Otherwise, it sets the response body with the default message associated to the code
# (eg 404 will set `"Not Found"`).
#
# @param code [Fixnum] a valid HTTP status code
# @param message [String] the response body
#
# @since 0.2.0
#
# @see Lotus::Controller#handled_exceptions
# @see Lotus::Action::Throwable#handle_exception
# @see Lotus::Http::Status:ALL
def halt(code = nil)
status(*Http::Status.for_code(code)) if code
def halt(code = nil, message = nil)
if code
message ||= Http::Status.for_code(code)[1]
status(code, message)
end
throw :halt
end

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures.rb
Expand Up @@ -365,7 +365,7 @@ class ThrowCodeAction
include Lotus::Action

def call(params)
halt params[:status]
halt params[:status], params[:message]
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/throw_test.rb
Expand Up @@ -37,6 +37,13 @@
end
end

it "throws an HTTP status code with given message" do
response = ThrowCodeAction.new.call({ status: 401, message: 'Secret Sauce' })

response[0].must_equal 401
response[2].must_equal ['Secret Sauce']
end

it 'throws the code as it is, when not recognized' do
response = ThrowCodeAction.new.call({ status: 2131231 })

Expand Down

0 comments on commit c9475e2

Please sign in to comment.