From cf64245c7dde5b568a163d585e356edf2d6f8ec5 Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Fri, 24 Jun 2022 12:06:26 -0300 Subject: [PATCH 1/2] fix(sql lab): Syntax errors should return with 422 status --- superset/sqllab/command.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/superset/sqllab/command.py b/superset/sqllab/command.py index ce41eb6de230f..e722353579bb1 100644 --- a/superset/sqllab/command.py +++ b/superset/sqllab/command.py @@ -26,7 +26,11 @@ from superset.common.db_query_status import QueryStatus from superset.dao.exceptions import DAOCreateFailedError from superset.errors import SupersetErrorType -from superset.exceptions import SupersetErrorsException, SupersetGenericErrorException +from superset.exceptions import ( + SupersetErrorsException, + SupersetException, + SupersetGenericErrorException, +) from superset.models.core import Database from superset.models.sql_lab import Query from superset.sqllab.command_status import SqlJsonExecutionStatus @@ -110,7 +114,13 @@ def run( # pylint: disable=too-many-statements,useless-suppression "status": status, "payload": self._execution_context_convertor.serialize_payload(), } - except (SqlLabException, SupersetErrorsException) as ex: + except SupersetErrorsException as ex: + if all(ex.error_type == SupersetErrorType.SYNTAX_ERROR for ex in ex.errors): + ex.status = 422 + raise ex + except SupersetException as ex: + if ex.error_type == SupersetErrorType.SYNTAX_ERROR: + ex.status = 422 raise ex except Exception as ex: raise SqlLabException(self._execution_context, exception=ex) from ex From 9fb87b13ff807eda9bfc6685c1d87cdb34b98b2a Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Mon, 27 Jun 2022 09:37:30 -0300 Subject: [PATCH 2/2] refactor --- superset/exceptions.py | 8 ++++++++ superset/sqllab/command.py | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/superset/exceptions.py b/superset/exceptions.py index 07bedfa2db568..153d7439eb790 100644 --- a/superset/exceptions.py +++ b/superset/exceptions.py @@ -115,6 +115,14 @@ def __init__( self.status = status +class SupersetSyntaxErrorException(SupersetErrorsException): + status = 422 + error_type = SupersetErrorType.SYNTAX_ERROR + + def __init__(self, errors: List[SupersetError]) -> None: + super().__init__(errors) + + class SupersetTimeoutException(SupersetErrorFromParamsException): status = 408 diff --git a/superset/sqllab/command.py b/superset/sqllab/command.py index e722353579bb1..0aeab754ca54c 100644 --- a/superset/sqllab/command.py +++ b/superset/sqllab/command.py @@ -25,11 +25,12 @@ from superset.commands.base import BaseCommand from superset.common.db_query_status import QueryStatus from superset.dao.exceptions import DAOCreateFailedError -from superset.errors import SupersetErrorType +from superset.errors import ErrorLevel, SupersetError, SupersetErrorType from superset.exceptions import ( SupersetErrorsException, SupersetException, SupersetGenericErrorException, + SupersetSyntaxErrorException, ) from superset.models.core import Database from superset.models.sql_lab import Query @@ -116,11 +117,19 @@ def run( # pylint: disable=too-many-statements,useless-suppression } except SupersetErrorsException as ex: if all(ex.error_type == SupersetErrorType.SYNTAX_ERROR for ex in ex.errors): - ex.status = 422 + raise SupersetSyntaxErrorException(ex.errors) from ex raise ex except SupersetException as ex: if ex.error_type == SupersetErrorType.SYNTAX_ERROR: - ex.status = 422 + raise SupersetSyntaxErrorException( + [ + SupersetError( + message=ex.message, + error_type=ex.error_type, + level=ErrorLevel.ERROR, + ) + ] + ) from ex raise ex except Exception as ex: raise SqlLabException(self._execution_context, exception=ex) from ex