Python osgeo.gdal.PushErrorHandler() Examples

The following are 1 code examples of osgeo.gdal.PushErrorHandler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module osgeo.gdal , or try the search function .
Example #1
Source File: helper_classes.py    From buzzard with Apache License 2.0 5 votes vote down vote up
def __call__(self, *args, **kwargs):
        errs, res = None, None

        def error_handler(err_level, err_no, err_msg):
            nonlocal errs
            if err_level >= gdal.CE_Failure:
                errs = err_no, err_msg

        gdal.PushErrorHandler(error_handler)
        try:
            res = self._fn(*args, **kwargs)
        except Exception as e:
            if errs is None:
                # This is not a gdal error, this might be a swig error or something else
                raise
            else:
                # This is a gdal error, and `gdal.GetUseExceptions()` is True
                # Read problems from the `errs` variable, the details stored in this exception
                # are not reliable.
                pass
        finally:
            gdal.PopErrorHandler()

        if errs:
            return False, errs
        if self._none_is_error and res is None:
            return False, (0, str(gdal.GetLastErrorMsg()).strip('\n'))
        if self._nonzero_int_is_error and isinstance(res, int) and res != 0:
            return False, (res, conv.str_of_cple(res))
        return True, res