Skip to content

Commit

Permalink
feat: more error messages (#15409)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Jun 28, 2021
1 parent e713912 commit 21d1fb5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
8 changes: 8 additions & 0 deletions docs/src/pages/docs/Miscellaneous/issue_codes.mdx
Expand Up @@ -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.
1 change: 1 addition & 0 deletions superset-frontend/src/components/ErrorMessage/types.ts
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions superset-frontend/src/setup/setupErrorMessages.ts
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions superset/errors.py
Expand Up @@ -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"
Expand Down Expand Up @@ -152,6 +153,15 @@ class SupersetErrorType(str, Enum):
),
},
],
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,
Expand Down
16 changes: 15 additions & 1 deletion superset/exceptions.py
Expand Up @@ -48,6 +48,19 @@ def __init__(self, error: SupersetError) -> None:
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"""

Expand Down Expand Up @@ -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,
)


Expand Down
23 changes: 19 additions & 4 deletions superset/views/core.py
Expand Up @@ -83,6 +83,7 @@
SupersetErrorsException,
SupersetException,
SupersetGenericDBErrorException,
SupersetGenericErrorException,
SupersetSecurityException,
SupersetTemplateParamsErrorException,
SupersetTimeoutException,
Expand Down Expand Up @@ -2540,7 +2541,12 @@ def sql_json_exec( # pylint: disable=too-many-statements,too-many-locals

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
Expand Down Expand Up @@ -2576,9 +2582,14 @@ def sql_json_exec( # pylint: disable=too-many-statements,too-many-locals
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)

Expand All @@ -2600,7 +2611,10 @@ def sql_json_exec( # pylint: disable=too-many-statements,too-many-locals
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"):
Expand All @@ -2619,6 +2633,7 @@ def sql_json_exec( # pylint: disable=too-many-statements,too-many-locals
)
+ " "
+ PARAMETER_MISSING_ERR,
error=SupersetErrorType.MISSING_TEMPLATE_PARAMS_ERROR,
extra={
"undefined_parameters": list(undefined_parameters),
"template_parameters": template_params,
Expand Down

0 comments on commit 21d1fb5

Please sign in to comment.