Sha256: 3e1a949aba2c3ea1c6bfb7eae36992a6ee285292b884007d11b435576fd1095f

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

require_relative 'exceptions'

module OGR
  # 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: http://www.gdal.org/ogr__core_8h.html).  This maps those
  # Symbols to Ruby exceptions (or lack thereof).  The sad part of this
  # solution is that any function that returns an OGRErr needs to assign that
  # Symbol to a variable, then call #handle_result to get that
  # Symbol-to-Exception mapping to take place.
  module ErrorHandling
    def handle_result(msg = nil)
      error_class_map(self).call(msg)
    end

    private

    # @param [Symbol] error_class
    # @return [Proc]
    def error_class_map(error_class)
      {
        OGRERR_NONE: ->(_) { true },
        OGRERR_NOT_ENOUGH_DATA: ->(msg) { raise_exception(OGR::NotEnoughData, msg) },
        OGRERR_NOT_ENOUGH_MEMORY: ->(msg) { raise_exception(::NoMemoryError, msg) },
        OGRERR_UNSUPPORTED_GEOMETRY_TYPE: ->(msg) { raise_exception(OGR::UnsupportedGeometryType, msg) },
        OGRERR_UNSUPPORTED_OPERATION: ->(msg) { raise_exception(OGR::UnsupportedOperation, msg) },
        OGRERR_CORRUPT_DATA: ->(msg) { raise_exception(OGR::CorruptData, msg) },
        OGRERR_FAILURE: ->(msg) { raise_exception(OGR::Failure, msg) },
        OGRERR_UNSUPPORTED_SRS: ->(msg) { raise_exception(OGR::UnsupportedSRS, msg) },
        OGRERR_INVALID_HANDLE: ->(msg) { raise_exception(OGR::InvalidHandle, msg) }
      }.fetch(error_class) { fail "Unknown OGRERR type: #{self}" }
    end

    # Exists solely to strip off the top 4 lines of the backtrace so it doesn't
    # look like the problem is coming from here.
    def raise_exception(exception, message)
      e = exception.new(message)
      e.set_backtrace(caller(4))
      fail(e)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ffi-gdal-1.0.0.beta5 lib/ogr/error_handling.rb
ffi-gdal-1.0.0.beta4 lib/ogr/error_handling.rb