Module: OGR::ErrorHandling

Defined in:
lib/ogr/error_handling.rb

Overview

OGR returns errors as Integers–not as part of the GDAL/CPLErr error handling callback interface. This hacks together a facility for sort of doing that with OGR.

Unlike the OGR API, ffi-gdal defines an Enum for the OGRERR types, which in turns causes OGR to return Symbols on errors (the #defines for those can be found here: www.gdal.org/ogr__core_8h.html). This maps those Symbols to Ruby exceptions (or lack thereof).

Constant Summary collapse

ERROR_CLASS_MAP =
{
  OGRERR_NONE: nil,
  OGRERR_NOT_ENOUGH_DATA: OGR::NotEnoughData,
  OGRERR_NOT_ENOUGH_MEMORY: ::NoMemoryError,
  OGRERR_UNSUPPORTED_GEOMETRY_TYPE: OGR::UnsupportedGeometryType,
  OGRERR_UNSUPPORTED_OPERATION: OGR::UnsupportedOperation,
  OGRERR_CORRUPT_DATA: OGR::CorruptData,
  OGRERR_FAILURE: OGR::Failure,
  OGRERR_UNSUPPORTED_SRS: OGR::UnsupportedSRS,
  OGRERR_INVALID_HANDLE: OGR::InvalidHandle
}.freeze

Class Method Summary collapse

Class Method Details

.handle_ogr_err(msg) ⇒ Object

Yields, then expects the result to be a Symbol from FFI::OGR::Core::Err.

Parameters:



30
31
32
33
34
35
36
# File 'lib/ogr/error_handling.rb', line 30

def self.handle_ogr_err(msg)
  ogr_err_symbol = yield

  klass = ERROR_CLASS_MAP.fetch(ogr_err_symbol) { raise "Unknown OGRERR type: #{self}" }

  raise_exception(klass, msg) if klass
end

.raise_exception(exception, message) ⇒ Object

Exists solely to strip off the top 4 lines of the backtrace so it doesn’t look like the problem is coming from here.



40
41
42
43
44
# File 'lib/ogr/error_handling.rb', line 40

def self.raise_exception(exception, message)
  e = exception.new(message)
  e.set_backtrace(caller(2))
  raise(e)
end