module Apia::EnvironmentErrorHandling

Public Instance Methods

error_for_exception(exception_class) click to toggle source

Return an error instance for a given exception class

@param exception_class [Class] any error class @return [Class, nil] any class that inherits from Apia::Error or nil if no error is found

# File lib/apia/environment_error_handling.rb, line 25
def error_for_exception(exception_class)
  potential_error_sources.each do |source|
    source.definition.potential_errors.each do |error|
      if error.definition.catchable_exceptions.key?(exception_class)
        return {
          error: error,
          block: error.definition.catchable_exceptions[exception_class]
        }
      end
    end
  end
  nil
end
raise_error(error, fields = {}) click to toggle source

Raise an error

@param error [String, Class] an error class or the name of a defined error

# File lib/apia/environment_error_handling.rb, line 9
def raise_error(error, fields = {})
  if error.respond_to?(:ancestors) && error.ancestors.include?(Apia::Error)
    raise error.exception(fields)
  end

  if found_error = find_error_by_name(error)
    raise found_error.exception(fields)
  end

  raise Apia::RuntimeError, "No error defined named #{error}"
end
raise_exception(exception) click to toggle source
# File lib/apia/environment_error_handling.rb, line 39
def raise_exception(exception)
  error = error_for_exception(exception.class)
  raise exception if error.nil?

  fields = {}
  error[:block]&.call(fields, exception)
  raise error[:error].exception(fields)
end

Private Instance Methods

find_error_by_name(error_name) click to toggle source
# File lib/apia/environment_error_handling.rb, line 50
def find_error_by_name(error_name)
  return nil if potential_error_sources.nil?

  potential_error_sources.each do |source|
    error = find_potential_error(source, error_name)
    return error if error
  end

  nil
end
find_potential_error(source, name) click to toggle source
# File lib/apia/environment_error_handling.rb, line 61
def find_potential_error(source, name)
  return nil if source.nil?

  unless name =~ /\//
    name = source.definition.id + '/' + name
  end

  source.definition.potential_errors.find do |error|
    error.definition.id == name
  end
end