Module: Kraftwerk::Endpoint::ErrorHandling

Defined in:
lib/kraftwerk/endpoint/error_handling.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/kraftwerk/endpoint/error_handling.rb', line 22

def self.prepended(base)
  base.class_eval do
    class_attribute :exception_handlers
    self.exception_handlers = {}
    extend ClassMethods
  end
end

Instance Method Details

#call(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kraftwerk/endpoint/error_handling.rb', line 4

def call(params)
  begin
    super
  rescue => exception
    if self.class.exception_handlers.key?(exception.class)
      handler = self.class.exception_handlers[exception.class]
      message = handler[:message].respond_to?(:call) ? handler[:message].call(exception) : handler[:message]
      unless message.is_a?(Kraftwerk::Response)
        message = Kraftwerk::Response.new(code: handler[:code], body: { error: message })
      end
      message
      # todo call app expection handler somehow
    else
      raise exception
    end
  end
end