Module: ManageIQ::API::Common::ApplicationControllerMixins::ExceptionHandling

Defined in:
lib/manageiq/api/common/application_controller_mixins/exception_handling.rb

Constant Summary collapse

DEFAULT_ERROR_CODE =
400

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/manageiq/api/common/application_controller_mixins/exception_handling.rb', line 8

def self.included(other)
  other.rescue_from(StandardError, RuntimeError) do |exception|
    errors = ManageIQ::API::Common::ErrorDocument.new.tap do |error_document|
      exception_list_from(exception).each do |exc|
        code = exc.respond_to?(:code) ? exc.code : error_code_from_class(exc)
        error_document.add(code, "#{exc.class}: #{exc.message}")
      end
    end

    render :json => errors.to_h, :status => error_code_from_class(exception)
  end
end

Instance Method Details

#error_code_from_class(exception) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/manageiq/api/common/application_controller_mixins/exception_handling.rb', line 30

def error_code_from_class(exception)
  if ActionDispatch::ExceptionWrapper.rescue_responses.key?(exception.class.to_s)
    ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.to_s]
  else
    DEFAULT_ERROR_CODE
  end
end

#exception_list_from(exception) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/manageiq/api/common/application_controller_mixins/exception_handling.rb', line 21

def exception_list_from(exception)
  [].tap do |arr|
    until exception.nil?
      arr << exception
      exception = exception.cause
    end
  end
end