feat: Better Errors in SQL Lab (#15432)

* snowflake errors

* added big query

* added to setup error messages, first test

* all big query testing added

* added snowflake test

* added syntax error

* added syntax errors to most used databases
This commit is contained in:
AAfghahi
2021-06-29 19:48:27 -04:00
committed by GitHub
parent 4a394cd6fb
commit 743d9cc928
16 changed files with 412 additions and 4 deletions

View File

@@ -15,18 +15,31 @@
# specific language governing permissions and limitations
# under the License.
import json
import re
from datetime import datetime
from typing import Optional, TYPE_CHECKING
from typing import Any, Dict, Optional, Pattern, Tuple, TYPE_CHECKING
from urllib import parse
from flask_babel import gettext as __
from sqlalchemy.engine.url import URL
from superset.db_engine_specs.postgres import PostgresBaseEngineSpec
from superset.errors import SupersetErrorType
from superset.utils import core as utils
if TYPE_CHECKING:
from superset.models.core import Database
# Regular expressions to catch custom errors
OBJECT_DOES_NOT_EXIST_REGEX = re.compile(
r"Object (?P<object>.*?) does not exist or not authorized."
)
SYNTAX_ERROR_REGEX = re.compile(
"syntax error line (?P<line>.+?) at position (?P<position>.+?) "
"unexpected '(?P<syntax_error>.+?)'."
)
class SnowflakeEngineSpec(PostgresBaseEngineSpec):
engine = "snowflake"
@@ -54,6 +67,22 @@ class SnowflakeEngineSpec(PostgresBaseEngineSpec):
"P1Y": "DATE_TRUNC('YEAR', {col})",
}
custom_errors: Dict[Pattern[str], Tuple[str, SupersetErrorType, Dict[str, Any]]] = {
OBJECT_DOES_NOT_EXIST_REGEX: (
__("%(object)s does not exist in this database."),
SupersetErrorType.OBJECT_DOES_NOT_EXIST_ERROR,
{},
),
SYNTAX_ERROR_REGEX: (
__(
"Please check your query for syntax errors at or "
'near "%(syntax_error)s". Then, try running your query again.'
),
SupersetErrorType.SYNTAX_ERROR,
{},
),
}
@classmethod
def adjust_database_uri(
cls, uri: URL, selected_schema: Optional[str] = None