Repository: incubator-airflow
Updated Branches:
  refs/heads/master 70f1bf10a -> f5462c78f


[AIRFLOW-947] Improve exceptions for unavailable Presto cluster

This improves error logging when the Presto cluster is unavailable
and the underlying error is a 503 http response. This introspects
the error to prevent trying to access the 'message' attribute when
not present.


Project: http://git-wip-us.apache.org/repos/asf/incubator-airflow/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-airflow/commit/6dd4b3bc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-airflow/tree/6dd4b3bc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-airflow/diff/6dd4b3bc

Branch: refs/heads/master
Commit: 6dd4b3bc1f366e1f4f4b42d6781f1caee7a5827a
Parents: 70f1bf1
Author: Arthur Wiedmer <[email protected]>
Authored: Mon Mar 6 21:06:13 2017 -0800
Committer: Arthur Wiedmer <[email protected]>
Committed: Tue Apr 4 09:37:22 2017 -0700

----------------------------------------------------------------------
 airflow/hooks/presto_hook.py | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/6dd4b3bc/airflow/hooks/presto_hook.py
----------------------------------------------------------------------
diff --git a/airflow/hooks/presto_hook.py b/airflow/hooks/presto_hook.py
index f8f6ac8..768ff3f 100644
--- a/airflow/hooks/presto_hook.py
+++ b/airflow/hooks/presto_hook.py
@@ -54,6 +54,19 @@ class PrestoHook(DbApiHook):
     def _strip_sql(sql):
         return sql.strip().rstrip(';')
 
+    def _get_pretty_exception_message(self, e):
+        """
+        Parses some DatabaseError to provide a better error message
+        """
+        if (hasattr(e, 'message')
+                and 'errorName' in e.message
+                and 'message' in e.message):
+            return ('{name}: {message}'.format(
+                    name=e.message['errorName'],
+                    message=e.message['message']))
+        else:
+            return str(e)
+
     def get_records(self, hql, parameters=None):
         """
         Get a set of records from Presto
@@ -62,14 +75,7 @@ class PrestoHook(DbApiHook):
             return super(PrestoHook, self).get_records(
                 self._strip_sql(hql), parameters)
         except DatabaseError as e:
-            if (hasattr(e, 'message') and
-                'errorName' in e.message and
-                'message' in e.message):
-                # Use the structured error data in the raised exception
-                raise PrestoException('{name}: {message}'.format(
-                    name=e.message['errorName'], message=e.message['message']))
-            else:
-                raise PrestoException(str(e))
+            raise PrestoException(self._parse_exception_message(e))
 
     def get_first(self, hql, parameters=None):
         """
@@ -80,7 +86,7 @@ class PrestoHook(DbApiHook):
             return super(PrestoHook, self).get_first(
                 self._strip_sql(hql), parameters)
         except DatabaseError as e:
-            raise PrestoException(e[0]['message'])
+            raise PrestoException(self._parse_exception_message(e))
 
     def get_pandas_df(self, hql, parameters=None):
         """
@@ -92,7 +98,7 @@ class PrestoHook(DbApiHook):
             cursor.execute(self._strip_sql(hql), parameters)
             data = cursor.fetchall()
         except DatabaseError as e:
-            raise PrestoException(e[0]['message'])
+            raise PrestoException(self._parse_exception_message(e))
         column_descriptions = cursor.description
         if data:
             df = pandas.DataFrame(data)

Reply via email to