Skip to content

Commit

Permalink
Update sql validator status (#8799)
Browse files Browse the repository at this point in the history
* fix: update status code for sql validator error

* fix: inspect error

* fix: lint issues

* fix: lint issues 2
  • Loading branch information
khtruong committed Dec 10, 2019
1 parent e6be519 commit 6c130b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
19 changes: 14 additions & 5 deletions superset/sql_validators/presto_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def validate_statement(
# invalid error, and restructure that error as an annotation we can
# return up.

# If the first element in the DatabaseError is not a dictionary, but
# is a string, return that message.
if db_error.args and isinstance(db_error.args[0], str):
raise PrestoSQLValidationError(db_error.args[0]) from db_error

# Confirm the first element in the DatabaseError constructor is a
# dictionary with error information. This is currently provided by
# the pyhive client, but may break if their interface changes when
Expand All @@ -107,12 +112,16 @@ def validate_statement(
"The pyhive presto client did not report an error message"
) from db_error
if "errorLocation" not in error_args:
raise PrestoSQLValidationError(
"The pyhive presto client did not report an error location"
) from db_error
# Pylint is confused about the type of error_args, despite the hints
# and checks above.
# pylint: disable=invalid-sequence-index
message = error_args["message"] + "\n(Error location unknown)"
# If we have a message but no error location, return the message and
# set the location as the beginning.
return SQLValidationAnnotation(
message=message, line_number=1, start_column=1, end_column=1
)

# Pylint is confused about the type of error_args, despite the hints
# and checks above.
# pylint: disable=invalid-sequence-index
message = error_args["message"]
err_loc = error_args["errorLocation"]
Expand Down
10 changes: 7 additions & 3 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2639,11 +2639,15 @@ def validate_sql_json(self):
except Exception as e:
logging.exception(e)
msg = _(
f"{validator.name} was unable to check your query.\nPlease "
"make sure that any services it depends on are available\n"
f"{validator.name} was unable to check your query.\n"
"Please recheck your query.\n"
f"Exception: {e}"
)
return json_error_response(f"{msg}")
# Return as a 400 if the database error message says we got a 4xx error
if re.search(r"([\W]|^)4\d{2}([\W]|$)", str(e)):
return json_error_response(f"{msg}", status=400)
else:
return json_error_response(f"{msg}")

def _sql_json_async(
self, session: Session, rendered_query: str, query: Query, expand_data: bool
Expand Down

0 comments on commit 6c130b3

Please sign in to comment.