mistercrunch closed pull request #4514: Added new exception class and start of 
better exception/error handling
URL: https://github.com/apache/incubator-superset/pull/4514
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/superset/connectors/base/views.py 
b/superset/connectors/base/views.py
index 42ce670270..7d54dcf22e 100644
--- a/superset/connectors/base/views.py
+++ b/superset/connectors/base/views.py
@@ -6,7 +6,7 @@
 
 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 a16baf1039..2bbcfcfac8 100644
--- a/superset/connectors/druid/models.py
+++ b/superset/connectors/druid/models.py
@@ -35,11 +35,12 @@
 
 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 73f8677293..200b047112 100644
--- a/superset/db_engine_specs.py
+++ b/superset/db_engine_specs.py
@@ -38,7 +38,8 @@
 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 0000000000..569a74b0ed
--- /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 ab3cef8a22..bb43edcf1b 100644
--- a/superset/utils.py
+++ b/superset/utils.py
@@ -45,6 +45,8 @@
 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 @@
 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 4330e7d7ee..2614fe8535 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -38,6 +38,7 @@
 )
 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():
@@ -1092,6 +1093,10 @@ def generate_json(self, datasource_type, datasource_id, 
form_data,
 
         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 9225e5da8f..c262c84dab 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -37,6 +37,7 @@
 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 674c6f4a67..fa1c7b0da1 100644
--- a/tests/utils_tests.py
+++ b/tests/utils_tests.py
@@ -12,11 +12,11 @@
 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,
 )
 
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to