Module: Lotus::Action::Throwable::ClassMethods

Defined in:
lib/lotus/action/throwable.rb

Overview

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Since:

  • 0.1.0



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lotus/action/throwable.rb', line 21

def self.extended(base)
  base.class_eval do
    include Utils::ClassAttribute

    # Action handled exceptions.
    #
    # When an handled exception is raised during #call execution, it will be
    # translated into the associated HTTP status.
    #
    # By default there aren't handled exceptions, all the errors are threaded
    # as a Server Side Error (500).
    #
    # @api private
    # @since 0.1.0
    #
    # @see Lotus::Controller.handled_exceptions
    # @see Lotus::Action::Throwable.handle_exception
    class_attribute :handled_exceptions
    self.handled_exceptions = Controller.handled_exceptions.dup
  end
end

Instance Method Details

#handle_exception(exception, status) ⇒ Object (protected)

Handle the given exception with an HTTP status code.

When the exception is raise during #call execution, it will be translated into the associated HTTP status.

This is a fine grained control, for a global configuration see Lotus::Action.handled_exceptions

Examples:

require 'lotus/controller'

class Show
  include Lotus::Action
  handle_exception RecordNotFound, 404

  def call(params)
    # ...
    raise RecordNotFound.new
  end
end

Show.new.call({id: 1}) # => [404, {}, ['Not Found']]

Parameters:

  • exception (Class)

    the exception class

  • status (Fixmun)

    a valid HTTP status

See Also:

  • Lotus::Action.handled_exceptions

Since:

  • 0.1.0



74
75
76
# File 'lib/lotus/action/throwable.rb', line 74

def handle_exception(exception, status)
  self.handled_exceptions[exception] = status
end