Module: Quonfig::OpenFeature::Errors

Defined in:
lib/quonfig/openfeature/errors.rb

Overview

Maps native Quonfig SDK errors to OpenFeature ErrorCode constants.

Constant Summary collapse

ErrorCode =
::OpenFeature::SDK::Provider::ErrorCode

Class Method Summary collapse

Class Method Details

.to_error_code(err) ⇒ String

Returns one of the ErrorCode constants.

Parameters:

  • err (Exception, String, nil)

Returns:

  • (String)

    one of the ErrorCode constants



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/quonfig/openfeature/errors.rb', line 15

def to_error_code(err)
  return ErrorCode::GENERAL if err.nil?

  # Class-based mapping is the most reliable signal.
  if defined?(::Quonfig::Errors::MissingDefaultError) && err.is_a?(::Quonfig::Errors::MissingDefaultError)
    return ErrorCode::FLAG_NOT_FOUND
  end

  if defined?(::Quonfig::Errors::TypeMismatchError) && err.is_a?(::Quonfig::Errors::TypeMismatchError)
    return ErrorCode::TYPE_MISMATCH
  end

  if (defined?(::Quonfig::Errors::UninitializedError) && err.is_a?(::Quonfig::Errors::UninitializedError)) ||
     (defined?(::Quonfig::Errors::InitializationTimeoutError) && err.is_a?(::Quonfig::Errors::InitializationTimeoutError))
    return ErrorCode::PROVIDER_NOT_READY
  end

  # Fallback: inspect the message text for keywords, matching the Node provider.
  msg = (err.respond_to?(:message) ? err.message : err.to_s).to_s.downcase
  return ErrorCode::FLAG_NOT_FOUND if msg.include?('not found') ||
                                      msg.include?('no value found') ||
                                      msg.include?('value found for key')
  return ErrorCode::TYPE_MISMATCH if msg.include?('type mismatch') ||
                                     msg.include?('expected ') && msg.include?('got ')
  return ErrorCode::PROVIDER_NOT_READY if msg.include?('not initialized') ||
                                          msg.include?('provider not ready') ||
                                          msg.include?("couldn't initialize") ||
                                          msg.include?('initialization timeout')

  ErrorCode::GENERAL
end