Module: Apia::EnvironmentErrorHandling

Included in:
LookupEnvironment, RequestEnvironment
Defined in:
lib/apia/environment_error_handling.rb

Instance Method Summary collapse

Instance Method Details

#error_for_exception(exception_class) ⇒ Class?

Return an error instance for a given exception class

Parameters:

  • exception_class (Class)

    any error class

Returns:

  • (Class, nil)

    any class that inherits from Apia::Error or nil if no error is found



25
26
27
28
29
30
31
32
33
34
35
36
37
# 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 = {}) ⇒ Object

Raise an error

Parameters:

  • error (String, Class)

    an error class or the name of a defined error

Raises:



9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object



39
40
41
42
43
44
45
46
# 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