You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2018/03/12 05:07:53 UTC

[incubator-superset] branch master updated: Added new exception class and start of better exception/error handling (#4514)

This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 2bc089e  Added new exception class and start of better exception/error handling (#4514)
2bc089e is described below

commit 2bc089ef8dcec5d98a43cb2ec80aacf1cdc706cf
Author: Hugh A. Miles II <hu...@gmail.com>
AuthorDate: Sun Mar 11 22:07:51 2018 -0700

    Added new exception class and start of better exception/error handling (#4514)
    
    * rebase and linting
    
    * change back
    
    * wip
    
    * fixed broken test
    
    * fix flake8
    
    * fix test
---
 superset/connectors/base/views.py   |  2 +-
 superset/connectors/druid/models.py |  3 ++-
 superset/db_engine_specs.py         |  3 ++-
 superset/exceptions.py              | 30 ++++++++++++++++++++++++++++++
 superset/utils.py                   | 26 ++------------------------
 superset/views/core.py              |  7 ++++++-
 superset/viz.py                     |  1 +
 tests/utils_tests.py                |  4 ++--
 8 files changed, 46 insertions(+), 30 deletions(-)

diff --git a/superset/connectors/base/views.py b/superset/connectors/base/views.py
index 42ce670..7d54dcf 100644
--- a/superset/connectors/base/views.py
+++ b/superset/connectors/base/views.py
@@ -6,7 +6,7 @@ from __future__ import unicode_literals
 
 from flask import Markup
 
-from superset.utils import SupersetException
+from superset.exceptions import SupersetException
 from superset.views.base import SupersetModelView
 
 
diff --git a/superset/connectors/druid/models.py b/superset/connectors/druid/models.py
index 0541af0..caef04b 100644
--- a/superset/connectors/druid/models.py
+++ b/superset/connectors/druid/models.py
@@ -35,11 +35,12 @@ from sqlalchemy.orm import backref, relationship
 
 from superset import conf, db, import_util, sm, utils
 from superset.connectors.base.models import BaseColumn, BaseDatasource, BaseMetric
+from superset.exceptions import MetricPermException
 from superset.models.helpers import (
     AuditMixinNullable, ImportMixin, QueryResult, set_perm,
 )
 from superset.utils import (
-    DimSelector, DTTM_ALIAS, flasher, MetricPermException,
+    DimSelector, DTTM_ALIAS, flasher,
 )
 
 DRUID_TZ = conf.get('DRUID_TZ')
diff --git a/superset/db_engine_specs.py b/superset/db_engine_specs.py
index 73f8677..200b047 100644
--- a/superset/db_engine_specs.py
+++ b/superset/db_engine_specs.py
@@ -38,7 +38,8 @@ import unicodecsv
 from werkzeug.utils import secure_filename
 
 from superset import app, cache_util, conf, db, utils
-from superset.utils import QueryStatus, SupersetTemplateException
+from superset.exceptions import SupersetTemplateException
+from superset.utils import QueryStatus
 
 config = app.config
 
diff --git a/superset/exceptions.py b/superset/exceptions.py
new file mode 100644
index 0000000..569a74b
--- /dev/null
+++ b/superset/exceptions.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+from __future__ import unicode_literals
+
+
+class SupersetException(Exception):
+    status = 500
+
+
+class SupersetTimeoutException(SupersetException):
+    pass
+
+
+class SupersetSecurityException(SupersetException):
+    pass
+
+
+class MetricPermException(SupersetException):
+    pass
+
+
+class NoDataException(SupersetException):
+    status = 400
+
+
+class SupersetTemplateException(SupersetException):
+    pass
diff --git a/superset/utils.py b/superset/utils.py
index ab3cef8..bb43edc 100644
--- a/superset/utils.py
+++ b/superset/utils.py
@@ -45,6 +45,8 @@ import sqlalchemy as sa
 from sqlalchemy import event, exc, select
 from sqlalchemy.types import TEXT, TypeDecorator
 
+from superset.exceptions import SupersetException, SupersetTimeoutException
+
 
 logging.getLogger('MARKDOWN').setLevel(logging.INFO)
 
@@ -53,30 +55,6 @@ EPOCH = datetime(1970, 1, 1)
 DTTM_ALIAS = '__timestamp'
 
 
-class SupersetException(Exception):
-    pass
-
-
-class SupersetTimeoutException(SupersetException):
-    pass
-
-
-class SupersetSecurityException(SupersetException):
-    pass
-
-
-class MetricPermException(SupersetException):
-    pass
-
-
-class NoDataException(SupersetException):
-    pass
-
-
-class SupersetTemplateException(SupersetException):
-    pass
-
-
 def can_access(sm, permission_name, view_name, user):
     """Protecting from has_access failing from missing perms/view"""
     if user.is_anonymous():
diff --git a/superset/views/core.py b/superset/views/core.py
index 3b18e82..ad06b25 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -38,6 +38,7 @@ from superset import (
 )
 from superset.connectors.connector_registry import ConnectorRegistry
 from superset.connectors.sqla.models import AnnotationDatasource, SqlaTable
+from superset.exceptions import SupersetException, SupersetSecurityException
 from superset.forms import CsvToDatabaseForm
 from superset.legacy import cast_form_data
 import superset.models.core as models
@@ -115,7 +116,7 @@ def check_ownership(obj, raise_if_false=True):
     if not obj:
         return False
 
-    security_exception = utils.SupersetSecurityException(
+    security_exception = SupersetSecurityException(
         "You don't have the rights to alter [{}]".format(obj))
 
     if g.user.is_anonymous():
@@ -1099,6 +1100,10 @@ class Superset(BaseSupersetView):
 
         try:
             payload = viz_obj.get_payload()
+        except SupersetException as se:
+            logging.exception(se)
+            return json_error_response(utils.error_msg_from_exception(se),
+                                       status=se.status)
         except Exception as e:
             logging.exception(e)
             return json_error_response(utils.error_msg_from_exception(e))
diff --git a/superset/viz.py b/superset/viz.py
index 9bbf538..756fedc 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -37,6 +37,7 @@ from six.moves import cPickle as pkl, reduce
 from superset import app, cache, get_manifest_file, utils
 from superset.utils import DTTM_ALIAS, merge_extra_filters
 
+
 config = app.config
 stats_logger = config.get('STATS_LOGGER')
 
diff --git a/tests/utils_tests.py b/tests/utils_tests.py
index 674c6f4..fa1c7b0 100644
--- a/tests/utils_tests.py
+++ b/tests/utils_tests.py
@@ -12,11 +12,11 @@ import uuid
 from mock import patch
 import numpy
 
+from superset.exceptions import SupersetException
 from superset.utils import (
     base_json_conv, datetime_f, json_int_dttm_ser, json_iso_dttm_ser,
     JSONEncodedDict, memoized, merge_extra_filters, merge_request_params,
-    parse_human_timedelta,
-    SupersetException, validate_json, zlib_compress, zlib_decompress_to_string,
+    parse_human_timedelta, validate_json, zlib_compress, zlib_decompress_to_string,
 )
 
 

-- 
To stop receiving notification emails like this one, please contact
maximebeauchemin@apache.org.