Module: Aws::Crt::Errors

Defined in:
lib/aws-crt/errors.rb

Overview

CRT Errors - includes utilities for mapping errors from CRT to Ruby Exceptions

Constant Summary collapse

AWS_TO_RUBY_ERROR_MAP =
{
  'AWS_ERROR_INVALID_INDEX' => IndexError,
  'AWS_ERROR_OOM' => NoMemoryError,
  'AWS_ERROR_UNIMPLEMENTED' => NotImplementedError,
  'AWS_ERROR_INVALID_ARGUMENT' => ArgumentError,
  'AWS_ERROR_SYS_CALL_FAILURE' => SystemCallError,
  'AWS_ERROR_DIVIDE_BY_ZERO' => ZeroDivisionError,
  'AWS_ERROR_HASHTBL_ITEM_NOT_FOUND' => KeyError
}.freeze

Class Method Summary collapse

Class Method Details

.add_error_constant(constant) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/aws-crt/errors.rb', line 58

def self.add_error_constant(constant)
  @const_set_mutex.synchronize do
    # Ensure the const was not defined while blocked by the mutex
    if error_const_set?(constant)
      const_get(constant)
    else
      error_class = Class.new(Aws::Crt::Error)
      const_set(constant, error_class)
    end
  end
end

.error_class(error_name) ⇒ Object

Get the error class for a given error_name



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aws-crt/errors.rb', line 35

def self.error_class(error_name)
  if AWS_TO_RUBY_ERROR_MAP.include? error_name
    return AWS_TO_RUBY_ERROR_MAP[error_name]
  end

  constant = error_class_constant(error_name)
  if error_const_set?(constant)
    # modeled error class exist
    # set code attribute
    const_get(constant)

  else
    add_error_constant(constant)
  end
end

.error_class_constant(error_name) ⇒ Object

Convert an error code to an error class name/constant. This requires filtering non-safe characters from the constant name and ensuring it begins with an uppercase letter.



54
55
56
# File 'lib/aws-crt/errors.rb', line 54

def self.error_class_constant(error_name)
  error_name.to_s.gsub(/AWS_ERROR_/, '').split('_').map(&:capitalize).join
end

.error_const_set?(constant) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/aws-crt/errors.rb', line 70

def self.error_const_set?(constant)
  # Purposefully not using #const_defined? as that method returns true
  # for constants not defined directly in the current module.
  constants.include?(constant.to_sym)
end

.raise_last_errorObject

Raises:



24
25
26
27
28
29
30
31
32
# File 'lib/aws-crt/errors.rb', line 24

def self.raise_last_error
  error_code = Aws::Crt::Native.last_error
  return if error_code.zero?

  error_name = Aws::Crt::Native.error_name(error_code)
  msg = Aws::Crt::Native.error_debug_str(error_code)
  Aws::Crt::Native.reset_error
  raise error_class(error_name), msg
end