module Insights::API::Common::ApplicationControllerMixins::ExceptionHandling

Constants

DEFAULT_ERROR_CODE

Public Class Methods

included(other) click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 8
def self.included(other)
  other.rescue_from(StandardError, RuntimeError) do |exception|
    rescue_from_handler(exception) do |error_document, exc|
      error_document.add(error_code_from_class(exc).to_s, "#{exc.class}: #{exc.message}")
    end
  end
end

Public Instance Methods

api_client_errors(exc, error_document) click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 63
def api_client_errors(exc, error_document)
  body = json_parsed_body(exc)
  if body.is_a?(Hash) && body.key?('errors') && body['errors'].is_a?(Array)
    body['errors'].each do |error|
      next unless error.key?('status') && error.key?('detail')

      error_document.add(error['status'], error['detail'])
    end
  else
    error_document.add(exc.code.to_s, exc.message )
  end
end
api_client_exception?(exc) click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 58
def api_client_exception?(exc)
  exc.respond_to?(:code) && exc.respond_to?(:response_body) && exc.respond_to?(:response_headers) &&
    !exc.response_body.nil?
end
custom_exception?(exception) click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 42
def custom_exception?(exception)
  Insights::API::Common::CustomExceptions::CUSTOM_EXCEPTION_LIST.include?(exception.class.to_s)
end
error_code_from_class(exception) click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 50
def error_code_from_class(exception)
  if ActionDispatch::ExceptionWrapper.rescue_responses.key?(exception.class.to_s)
    Rack::Utils.status_code(ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.to_s])
  else
    DEFAULT_ERROR_CODE
  end
end
exception_list_from(exception) click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 33
def exception_list_from(exception)
  [].tap do |arr|
    until exception.nil?
      arr << exception
      exception = exception.cause
    end
  end
end
fetch_custom_message(exception) click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 46
def fetch_custom_message(exception)
  Insights::API::Common::CustomExceptions.custom_message(exception)
end
json_parsed_body(exc) click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 76
def json_parsed_body(exc)
  JSON.parse(exc.response_body)
rescue StandardError
  nil
end
rescue_from_handler(exception) { |error_document, exc| ... } click to toggle source
# File lib/insights/api/common/application_controller_mixins/exception_handling.rb, line 16
def rescue_from_handler(exception)
  errors = Insights::API::Common::ErrorDocument.new.tap do |error_document|
    exception_list_from(exception).each do |exc|
      if api_client_exception?(exc)
        api_client_errors(exc, error_document)
      elsif custom_exception?(exc)
        message = fetch_custom_message(exc)
        error_document.add(error_code_from_class(exc).to_s, message)
      else
        yield error_document, exc
      end
    end
  end

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