Skip to content

Commit

Permalink
[sql lab] improve error messages (#3308)
Browse files Browse the repository at this point in the history
* [sql lab] improve error messages

Some error messages in non-async mode were showing as JSON or sometimes
as tuples. This fixes that.

* linting
  • Loading branch information
mistercrunch authored Aug 18, 2017
1 parent 2923a12 commit 6fc837d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions superset/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,10 +950,10 @@ def load_random_time_series_data():

def load_country_map_data():
"""Loading data for map with country map"""
csvPath = os.path.join(DATA_FOLDER, 'birth_france_data_for_country_map.csv')
data = pd.read_csv(csvPath, encoding="utf-8")
csv_path = os.path.join(DATA_FOLDER, 'birth_france_data_for_country_map.csv')
data = pd.read_csv(csv_path, encoding="utf-8")
data['date'] = datetime.datetime.now().date()
data.to_sql(
data.to_sql( # pylint: disable=no-member
'birth_france_by_region',
db.engine,
if_exists='replace',
Expand Down
11 changes: 11 additions & 0 deletions superset/db_engine_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ def adjust_database_uri(cls, uri, selected_schema=None):
def epoch_to_dttm(cls):
return "from_unixtime({col})"

@classmethod
def extract_error_message(cls, e):
"""Extract error message for queries"""
message = str(e)
try:
if isinstance(e.args, tuple) and len(e.args) > 1:
message = e.args[1]
except:
pass
return message


class PrestoEngineSpec(BaseEngineSpec):
engine = 'presto'
Expand Down
2 changes: 1 addition & 1 deletion superset/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def handle_error(msg):
session.commit()
payload.update({
'status': query.status,
'error_essage': msg,
'error': msg,
})
return payload

Expand Down
12 changes: 6 additions & 6 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def get_error_msg():
return error_msg


def json_error_response(msg, status=None, stacktrace=None):
data = {'error': str(msg)}
if stacktrace:
data['stacktrace'] = stacktrace
status = status if status else 500
def json_error_response(msg=None, status=500, stacktrace=None, payload=None):
if not payload:
payload = {'error': str(msg)}
if stacktrace:
payload['stacktrace'] = stacktrace
return Response(
json.dumps(data),
json.dumps(payload, default=utils.json_iso_dttm_ser),
status=status, mimetype="application/json")


Expand Down
5 changes: 2 additions & 3 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2074,13 +2074,12 @@ def sql_json(self):
# pylint: disable=no-value-for-parameter
data = sql_lab.get_sql_results(
query_id=query_id, return_results=True)
payload = json.dumps(data, default=utils.json_iso_dttm_ser)
except Exception as e:
logging.exception(e)
return json_error_response("{}".format(e))
if data.get('status') == QueryStatus.FAILED:
return json_error_response(payload)
return json_success(payload)
return json_error_response(payload=data)
return json_success(json.dumps(data, default=utils.json_iso_dttm_ser))

@has_access
@expose("/csv/<client_id>")
Expand Down

0 comments on commit 6fc837d

Please sign in to comment.