h3-py icon indicating copy to clipboard operation
h3-py copied to clipboard

v4 error handling

Open ajfriend opened this issue 3 years ago • 1 comments

We'd like to simplify the error checking and handling in h3-py v4 by:

  • doing fewer correctness tests in Python/Cython code (as long as that won't allow for segfaults), deferring those correctness tests to the user (with the idea that they'd know better when those checks are and are not necessary)
  • pass through the H3 core error values and messages, rather than using custom Python error types and messages

For the initial v4.0 release, we might do something simple like have only a single H3 error type (as opposed to a class hierarchy of errors) that can take on various messages, and might programmatically expose the H3 core error enum.

PRs or Issues providing examples to consider

  • #241

ajfriend avatar May 25 '22 03:05 ajfriend

One idea is to have a generic wrapper that captures and raises an error (from an H3 c library function that returns an error code), which also accepts extra arguments if that information would be useful to the user.

For example, the current Cython code:

cpdef int distance(H3int h1, H3int h2) except -1:
    """ Compute the grid distance between two cells
    """
    cdef:
        int64_t distance
        h3lib.H3Error err

    check_cell(h1)
    check_cell(h2)

    err = h3lib.gridDistance(h1, h2, &distance)
    if err:
        # todo: do error handling later
        s = 'Cells are too far apart to compute distance: {} and {}'
        s = s.format(hex(h1), hex(h2))
        raise H3ValueError(s)

    return distance

might instead look like:

cpdef int distance(H3int h1, H3int h2) except -1:
    """ Compute the grid distance between two cells
    """
    cdef:
        int64_t distance

    check_for_error(
        h3lib.gridDistance(h1, h2, &distance),
        h1,
        h2,
    )

    return distance

ajfriend avatar May 25 '22 04:05 ajfriend