diff --git a/docs/src/pages/docs/Miscellaneous/issue_codes.mdx b/docs/src/pages/docs/Miscellaneous/issue_codes.mdx index b51ce9b248..df8b65f06f 100644 --- a/docs/src/pages/docs/Miscellaneous/issue_codes.mdx +++ b/docs/src/pages/docs/Miscellaneous/issue_codes.mdx @@ -263,3 +263,11 @@ The database is currently running too many queries. ``` The database might be under heavy load, running too many queries. Please try again later, or contact an administrator for further assistance. + +## Issue 1028 + +``` +One or more parameters specified in the query are malformatted. +``` + +The query contains one or more malformed template parameters. Please check your query and confirm that all template parameters are surround by double braces, for example, "{{ ds }}". Then, try running your query again. diff --git a/superset-frontend/src/components/ErrorMessage/types.ts b/superset-frontend/src/components/ErrorMessage/types.ts index e2d2f29233..4f16d385e9 100644 --- a/superset-frontend/src/components/ErrorMessage/types.ts +++ b/superset-frontend/src/components/ErrorMessage/types.ts @@ -57,6 +57,7 @@ export const ErrorTypeEnum = { // Sqllab error MISSING_TEMPLATE_PARAMS_ERROR: 'MISSING_TEMPLATE_PARAMS_ERROR', + INVALID_TEMPLATE_PARAMS_ERROR: 'INVALID_TEMPLATE_PARAMS_ERROR', RESULTS_BACKEND_NOT_CONFIGURED_ERROR: 'RESULTS_BACKEND_NOT_CONFIGURED_ERROR', DML_NOT_ALLOWED_ERROR: 'DML_NOT_ALLOWED_ERROR', INVALID_CTAS_QUERY_ERROR: 'INVALID_CTAS_QUERY_ERROR', diff --git a/superset-frontend/src/setup/setupErrorMessages.ts b/superset-frontend/src/setup/setupErrorMessages.ts index ab2fb2d83a..022edeed34 100644 --- a/superset-frontend/src/setup/setupErrorMessages.ts +++ b/superset-frontend/src/setup/setupErrorMessages.ts @@ -51,6 +51,10 @@ export default function setupErrorMessages() { ErrorTypeEnum.MISSING_TEMPLATE_PARAMS_ERROR, ParameterErrorMessage, ); + errorMessageComponentRegistry.registerValue( + ErrorTypeEnum.INVALID_TEMPLATE_PARAMS_ERROR, + ParameterErrorMessage, + ); errorMessageComponentRegistry.registerValue( ErrorTypeEnum.RESULTS_BACKEND_NOT_CONFIGURED_ERROR, DatabaseErrorMessage, diff --git a/superset/errors.py b/superset/errors.py index 02f4207942..d50030c1eb 100644 --- a/superset/errors.py +++ b/superset/errors.py @@ -66,6 +66,7 @@ class SupersetErrorType(str, Enum): # Sql Lab errors MISSING_TEMPLATE_PARAMS_ERROR = "MISSING_TEMPLATE_PARAMS_ERROR" + INVALID_TEMPLATE_PARAMS_ERROR = "INVALID_TEMPLATE_PARAMS_ERROR" RESULTS_BACKEND_NOT_CONFIGURED_ERROR = "RESULTS_BACKEND_NOT_CONFIGURED_ERROR" DML_NOT_ALLOWED_ERROR = "DML_NOT_ALLOWED_ERROR" INVALID_CTAS_QUERY_ERROR = "INVALID_CTAS_QUERY_ERROR" @@ -152,6 +153,15 @@ ERROR_TYPES_TO_ISSUE_CODES_MAPPING = { ), }, ], + SupersetErrorType.INVALID_TEMPLATE_PARAMS_ERROR: [ + { + "code": 1028, + "message": _( + "Issue 1028 - One or more parameters specified in the query are " + "malformatted." + ), + }, + ], SupersetErrorType.RESULTS_BACKEND_NOT_CONFIGURED_ERROR: [ { "code": 1021, diff --git a/superset/exceptions.py b/superset/exceptions.py index cde7c3f44a..f22f359a3e 100644 --- a/superset/exceptions.py +++ b/superset/exceptions.py @@ -48,6 +48,19 @@ class SupersetErrorException(SupersetException): self.error = error +class SupersetGenericErrorException(SupersetErrorException): + """Exceptions that are too generic to have their own type""" + + def __init__(self, message: str) -> None: + super().__init__( + SupersetError( + message=message, + error_type=SupersetErrorType.GENERIC_BACKEND_ERROR, + level=ErrorLevel.ERROR, + ) + ) + + class SupersetErrorFromParamsException(SupersetErrorException): """Exceptions that pass in parameters to construct a SupersetError""" @@ -97,11 +110,12 @@ class SupersetTemplateParamsErrorException(SupersetErrorFromParamsException): def __init__( self, message: str, + error: SupersetErrorType, level: ErrorLevel = ErrorLevel.ERROR, extra: Optional[Dict[str, Any]] = None, ) -> None: super().__init__( - SupersetErrorType.MISSING_TEMPLATE_PARAMS_ERROR, message, level, extra, + error, message, level, extra, ) diff --git a/superset/views/core.py b/superset/views/core.py index f86f5c9eea..3be231475c 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -83,6 +83,7 @@ from superset.exceptions import ( SupersetErrorsException, SupersetException, SupersetGenericDBErrorException, + SupersetGenericErrorException, SupersetSecurityException, SupersetTemplateParamsErrorException, SupersetTimeoutException, @@ -2540,7 +2541,12 @@ class Superset(BaseSupersetView): # pylint: disable=too-many-public-methods mydb = session.query(Database).get(database_id) if not mydb: - return json_error_response("Database with id %i is missing.", database_id) + raise SupersetGenericErrorException( + _( + "The database referenced in this query was not found. Please " + "contact an administrator for further assistance or try again." + ) + ) # Set tmp_schema_name for CTA # TODO(bkyryliuk): consider parsing, splitting tmp_schema_name from @@ -2576,9 +2582,14 @@ class Superset(BaseSupersetView): # pylint: disable=too-many-public-methods except SQLAlchemyError as ex: logger.error("Errors saving query details %s", str(ex), exc_info=True) session.rollback() - raise Exception(_("Query record was not created as expected.")) + query_id = None if not query_id: - raise Exception(_("Query record was not created as expected.")) + raise SupersetGenericErrorException( + _( + "The query record was not created as expected. Please " + "contact an administrator for further assistance or try again." + ) + ) logger.info("Triggering query_id: %i", query_id) @@ -2600,7 +2611,10 @@ class Superset(BaseSupersetView): # pylint: disable=too-many-public-methods query.status = QueryStatus.FAILED session.commit() raise SupersetTemplateParamsErrorException( - utils.error_msg_from_exception(ex) + message=_( + 'The query contains one or more malformed template parameters. Please check your query and confirm that all template parameters are surround by double braces, for example, "{{ ds }}". Then, try running your query again.' + ), + error=SupersetErrorType.INVALID_TEMPLATE_PARAMS_ERROR, ) if is_feature_enabled("ENABLE_TEMPLATE_PROCESSING"): @@ -2619,6 +2633,7 @@ class Superset(BaseSupersetView): # pylint: disable=too-many-public-methods ) + " " + PARAMETER_MISSING_ERR, + error=SupersetErrorType.MISSING_TEMPLATE_PARAMS_ERROR, extra={ "undefined_parameters": list(undefined_parameters), "template_parameters": template_params,