Skip to content

Commit

Permalink
Added new exception class and start of better exception/error handling (
Browse files Browse the repository at this point in the history
apache#4514)

* rebase and linting

* change back

* wip

* fixed broken test

* fix flake8

* fix test
  • Loading branch information
hughhhh authored and mistercrunch committed Mar 12, 2018
1 parent f0a8301 commit 7c5703b
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion superset/connectors/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from flask import Markup

from superset.utils import SupersetException
from superset.exceptions import SupersetException
from superset.views.base import SupersetModelView


Expand Down
3 changes: 2 additions & 1 deletion superset/connectors/druid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion superset/db_engine_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions superset/exceptions.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 2 additions & 24 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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():
Expand Down
7 changes: 6 additions & 1 deletion superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -1099,6 +1100,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))
Expand Down
1 change: 1 addition & 0 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
4 changes: 2 additions & 2 deletions tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down

0 comments on commit 7c5703b

Please sign in to comment.