Python/PyHive - extract specific error message from exception

2.1k Views Asked by At

I'm facing an exception similar to this one and I'm trying to handle it based on the error itself. The problem is that pyhive.exc.OperationalError is very generic and handles errors from timeouts to non-existent tables so I would need the exact value from the errorMessage part in order to handle each error type differently, like if it's a timeout, wait and retry; if it's something else, handle it differently and so on.

If I catch the error as except OperationalError as e, how would I extract the errorMessage part? I could parse the string representation (e.__str__()) but that seems weird as I'm sure there's a proper way.

3

There are 3 best solutions below

0
On

you can extract the errorMessage this way

from pyhive import hive, exc

try:
  connection = hive.connect(host="localhost", port=10000)
  cursor = connection.cursor()
  cursor.execute(sql_string)
except exc.Error as e:
  error_message = e.args[0].status.errorMessage
  print(error_message)

you can use dir(e) to find the methods and dunder methods for each object. which will allow you to see what methods you can call to access the data.

0
On

If you see the implementation hive Exception implementation, It simply inherited Exception So it has exception details in the format of string only that looks like somewhat json that actually confuse us and we hope to access it using an instance variable or as a dictionary but in reality it is just string that looks like json .

But We can use the reguler expression to extract details as we wish like errortype etc.

0
On

I found one other way to do it, That might seems a much better way.

 try:
        return presto.execute_query_safe(query, bind_params)
 except pyhive.exc.DatabaseError as e:
        logging.error("Query failed", exc_info=1)
        if error.args[0]['errorName'] == 'SYNTAX_ERROR' or error.args[0]['errorType'] == 'USER_ERROR':
            raise NonRecoverableQueryError('Non Recoverable Error: ' + str(error))
        raise e