Add more Exception classes for common exceptions

Moved from GitHub pydgraph/110

Posted by dhagrow:

It would be helpful to have a more Exception classes to make it easier to handle several common error cases. Currently, grpc.StatusCode.ABORTED is raised as AbortedError, but only by Txn.mutate() and Txn.commit(). To handle an aborted transaction for DgraphClient.alter(), the following is still required:

try:
    client.alter(...)
except grpc.RpcError as e:
    if e.code() == grpc.StatusCode.ABORTED:
        # handle
    else:
        raise

Other errors may not even have a StatusCode, and require something like this:

try:
    client.alter(...)
except grpc.RpcError as e:
    if e.code() == grpc.StatusCode.UNKNOWN:
        if e.details().startswith('Pending transactions found'):
            # handle
        else:
            raise
    else:
        raise

I frequently see StatusCode.ABORTED, StatusCode.UNKNOWN (in my case, pending transactions), and StatusCode.DEADLINE_EXCEEDED (timeouts). Each of these can be resolved by retrying the operation, so it’s necessary to distinguish them from other error cases. It would be helpful if these could be caught as follows:

try:
    client.alter(...)
except pydgraph.errors.PendingTransactionError:
    # handle

vancouverwill commented :

there are a couple of other places where a more specific Error type would be very helpful and would avoid clients of this library having to do checks on the error message for example

I would be happy to make a PR if no else is planning on working on this, presumably pydgraph/errors.py at 6058634bd0635acb01d192e2bc7f57bbb6414e06 · dgraph-io/pydgraph · GitHub will be the right place, is there a preferred naming conventions for errors or names already chosen?

Thanks Will

martinmr commented :

Some basic exceptions have been introduced in #135. Feel free to open an issue if there’s an additional exception that should be handled with a special Exception.

These changes will be available in the next release of pydgraph.