class Oso::Polar::FFI::Error

Wrapper class for Error FFI pointer + operations.

Wrapper class for Error FFI pointer + operations.

Constants

Rust

Public Class Methods

get(enrich_message) click to toggle source

Check for an FFI error and convert it into a Ruby exception.

@return [::Oso::Polar::Error] if there's an FFI error. @return [::Oso::Polar::FFIErrorNotFound] if there isn't one.

# File lib/oso/polar/ffi/error.rb, line 27
def self.get(enrich_message) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  error = Rust.get
  return ::Oso::Polar::FFIErrorNotFound if error.null?

  error = JSON.parse(error.to_s)
  msg = error['formatted']
  kind, body = error['kind'].first

  # Not all errors have subkind and details.
  # TODO (gj): This bug may exist in other libraries.
  if body.is_a? Hash
    subkind, details = body.first
  else
    subkind, details = nil
  end

  # Enrich error message and stack trace
  msg = enrich_message.call(msg) if msg
  if details
    details['stack_trace'] = enrich_message.call(details['stack_trace']) if details['stack_trace']
    details['msg'] = enrich_message.call(details['msg']) if details['msg']
  end

  case kind
  when 'Parse'
    parse_error(subkind, msg: msg, details: details)
  when 'Runtime'
    runtime_error(subkind, msg: msg, details: details)
  when 'Operational'
    operational_error(subkind, msg: msg, details: details)
  when 'Parameter'
    api_error(subkind, msg: msg, details: details)
  when 'RolesValidation'
    validation_error(msg, details: details)
  end
end
release(ptr) click to toggle source
# File lib/oso/polar/ffi.rb, line 39
def self.release(ptr)
  Rust.free(ptr) unless ptr.null?
end

Private Class Methods

api_error(kind, msg:, details:) click to toggle source

Map FFI API errors into Ruby exceptions.

@param kind [String] @param msg [String] @param details [Hash<String, Object>] @return [::Oso::Polar::ApiError] the object converted into the expected format.

# File lib/oso/polar/ffi/error.rb, line 133
                     def self.api_error(kind, msg:, details:)
  case kind
  when 'Parameter'
    ::Oso::Polar::ParameterError.new(msg, details: details)
  else
    ::Oso::Polar::ApiError.new(msg, details: details)
  end
end
operational_error(kind, msg:, details:) click to toggle source

Map FFI operational errors into Ruby exceptions.

@param kind [String] @param msg [String] @param details [Hash<String, Object>] @return [::Oso::Polar::OperationalError] the object converted into the expected format.

# File lib/oso/polar/ffi/error.rb, line 118
                     def self.operational_error(kind, msg:, details:)
  case kind
  when 'Unknown' # Rust panics.
    ::Oso::Polar::UnknownError.new(msg, details: details)
  else
    ::Oso::Polar::OperationalError.new(msg, details: details)
  end
end
parse_error(kind, msg:, details:) click to toggle source

Map FFI parse errors into Ruby exceptions.

@param kind [String] @param msg [String] @param details [Hash<String, Object>] @return [::Oso::Polar::ParseError] the object converted into the expected format.

# File lib/oso/polar/ffi/error.rb, line 70
                     def self.parse_error(kind, msg:, details:) # rubocop:disable Metrics/MethodLength
  case kind
  when 'ExtraToken'
    ::Oso::Polar::ParseError::ExtraToken.new(msg, details: details)
  when 'IntegerOverflow'
    ::Oso::Polar::ParseError::IntegerOverflow.new(msg, details: details)
  when 'InvalidToken'
    ::Oso::Polar::ParseError::InvalidToken.new(msg, details: details)
  when 'InvalidTokenCharacter'
    ::Oso::Polar::ParseError::InvalidTokenCharacter.new(msg, details: details)
  when 'UnrecognizedEOF'
    ::Oso::Polar::ParseError::UnrecognizedEOF.new(msg, details: details)
  when 'UnrecognizedToken'
    ::Oso::Polar::ParseError::UnrecognizedToken.new(msg, details: details)
  else
    ::Oso::Polar::ParseError.new(msg, details: details)
  end
end
runtime_error(kind, msg:, details:) click to toggle source

Map FFI runtime errors into Ruby exceptions.

@param kind [String] @param msg [String] @param details [Hash<String, Object>] @return [::Oso::Polar::PolarRuntimeError] the object converted into the expected format.

# File lib/oso/polar/ffi/error.rb, line 95
                     def self.runtime_error(kind, msg:, details:) # rubocop:disable Metrics/MethodLength
  case kind
  when 'Serialization'
    ::Oso::Polar::SerializationError.new(msg, details: details)
  when 'Unsupported'
    ::Oso::Polar::UnsupportedError.new(msg, details: details)
  when 'TypeError'
    ::Oso::Polar::PolarTypeError.new(msg, details: details)
  when 'StackOverflow'
    ::Oso::Polar::StackOverflowError.new(msg, details: details)
  when 'FileLoading'
    ::Oso::Polar::FileLoadingError.new(msg, details: details)
  else
    ::Oso::Polar::PolarRuntimeError.new(msg, details: details)
  end
end
validation_error(msg, details:) click to toggle source

Map FFI Validation errors into Ruby exceptions.

@param msg [String] @param details [Hash<String, Object>] @return [::Oso::Polar::ValidationError] the object converted into the expected format.

# File lib/oso/polar/ffi/error.rb, line 147
                     def self.validation_error(msg, details:)
  # This is currently the only type of validation error.
  ::Oso::Polar::RolesValidationError.new(msg, details: details)
end

Public Instance Methods

to_s() click to toggle source
# File lib/oso/polar/ffi/error.rb, line 10
def to_s
  @to_s ||= read_string.force_encoding('UTF-8')
end