module Hanami::Action::Throwable

Throw API

@since 0.1.0

@see Hanami::Action::Throwable::ClassMethods#handle_exception @see Hanami::Action::Throwable#halt @see Hanami::Action::Throwable#status

Public Class Methods

included(base) click to toggle source

@since 0.1.0 @api private

# File lib/hanami/action/throwable.rb, line 17
def self.included(base)
  base.extend ClassMethods
end

Protected Instance Methods

halt(code, message = nil) click to toggle source

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.

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 Hanami::Controller#handled_exceptions @see Hanami::Action::Throwable#handle_exception @see Hanami::Http::Status:ALL

@example Basic usage

require 'hanami/controller'

class Show
  def call(params)
    halt 404
  end
end

# => [404, {}, ["Not Found"]]

@example Custom message

require 'hanami/controller'

class Show
  def call(params)
    halt 404, "This is not the droid you're looking for."
  end
end

# => [404, {}, ["This is not the droid you're looking for."]]
# File lib/hanami/action/throwable.rb, line 104
def halt(code, message = nil)
  message ||= Http::Status.message_for(code)
  status(code, message)

  throw :halt
end
status(code, message) click to toggle source

Sets the given code and message for the response

@param code [Fixnum] a valid HTTP status code @param message [String] the response body

@since 0.1.0 @see Hanami::Http::Status:ALL

# File lib/hanami/action/throwable.rb, line 118
def status(code, message)
  self.status = code
  self.body   = message
end

Private Instance Methods

_exception_handler(exception) click to toggle source

@since 0.3.0 @api private

# File lib/hanami/action/throwable.rb, line 158
def _exception_handler(exception)
  handler = configuration.exception_handler(exception)

  if respond_to?(handler.to_s, true)
    method(handler)
  else
    ->(ex) { halt handler }
  end
end
_handle_exception(exception) click to toggle source

@since 0.1.0 @api private

# File lib/hanami/action/throwable.rb, line 147
def _handle_exception(exception)
  raise unless configuration.handle_exceptions

  instance_exec(
    exception,
    &_exception_handler(exception)
  )
end
_reference_in_rack_errors(exception) click to toggle source

@since 0.2.0 @api private

# File lib/hanami/action/throwable.rb, line 139
def _reference_in_rack_errors(exception)
  return if configuration.handled_exception?(exception)

  Hanami::Action::Rack::Errors.set(@_env, exception)
end
_rescue() { || ... } click to toggle source

@since 0.1.0 @api private

# File lib/hanami/action/throwable.rb, line 126
def _rescue
  catch :halt do
    begin
      yield
    rescue => exception
      _reference_in_rack_errors(exception)
      _handle_exception(exception)
    end
  end
end