mirror of
https://github.com/apache/superset.git
synced 2026-04-20 00:24:38 +00:00
feat: add extract_errors to Postgres (#13997)
* feat: add extract_errors to Postgres * Add unit tests * Fix lint * Fix unit tests
This commit is contained in:
@@ -25,7 +25,7 @@ from superset.commands.exceptions import (
|
||||
ImportFailedError,
|
||||
UpdateFailedError,
|
||||
)
|
||||
from superset.exceptions import SupersetErrorException
|
||||
from superset.exceptions import SupersetErrorsException
|
||||
|
||||
|
||||
class DatabaseInvalidError(CommandInvalidError):
|
||||
@@ -117,26 +117,22 @@ class DatabaseDeleteFailedReportsExistError(DatabaseDeleteFailedError):
|
||||
message = _("There are associated alerts or reports")
|
||||
|
||||
|
||||
class DatabaseTestConnectionFailedError(CommandException):
|
||||
class DatabaseTestConnectionFailedError(SupersetErrorsException):
|
||||
status = 422
|
||||
message = _("Connection failed, please check your connection settings")
|
||||
|
||||
|
||||
class DatabaseSecurityUnsafeError(DatabaseTestConnectionFailedError):
|
||||
class DatabaseSecurityUnsafeError(CommandInvalidError):
|
||||
message = _("Stopped an unsafe database connection")
|
||||
|
||||
|
||||
class DatabaseTestConnectionDriverError(DatabaseTestConnectionFailedError):
|
||||
class DatabaseTestConnectionDriverError(CommandInvalidError):
|
||||
message = _("Could not load database driver")
|
||||
|
||||
|
||||
class DatabaseTestConnectionUnexpectedError(DatabaseTestConnectionFailedError):
|
||||
class DatabaseTestConnectionUnexpectedError(CommandInvalidError):
|
||||
message = _("Unexpected error occurred, please check your logs for details")
|
||||
|
||||
|
||||
class DatabaseImportError(ImportFailedError):
|
||||
message = _("Import database failed for an unknown reason")
|
||||
|
||||
|
||||
class DatabaseTestConnectionNetworkError(SupersetErrorException):
|
||||
status = 400
|
||||
|
||||
@@ -20,7 +20,6 @@ from typing import Any, Dict, Optional
|
||||
|
||||
from flask_appbuilder.security.sqla.models import User
|
||||
from flask_babel import gettext as _
|
||||
from sqlalchemy.engine.url import make_url
|
||||
from sqlalchemy.exc import DBAPIError, NoSuchModuleError
|
||||
|
||||
from superset.commands.base import BaseCommand
|
||||
@@ -28,15 +27,12 @@ from superset.databases.commands.exceptions import (
|
||||
DatabaseSecurityUnsafeError,
|
||||
DatabaseTestConnectionDriverError,
|
||||
DatabaseTestConnectionFailedError,
|
||||
DatabaseTestConnectionNetworkError,
|
||||
DatabaseTestConnectionUnexpectedError,
|
||||
)
|
||||
from superset.databases.dao import DatabaseDAO
|
||||
from superset.errors import ErrorLevel, SupersetErrorType
|
||||
from superset.exceptions import SupersetSecurityException
|
||||
from superset.extensions import event_logger
|
||||
from superset.models.core import Database
|
||||
from superset.utils.network import is_host_up, is_hostname_valid, is_port_open
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -47,53 +43,6 @@ class TestConnectionDatabaseCommand(BaseCommand):
|
||||
self._properties = data.copy()
|
||||
self._model: Optional[Database] = None
|
||||
|
||||
@staticmethod
|
||||
def _diagnose(uri: str) -> None:
|
||||
parsed_uri = make_url(uri)
|
||||
if parsed_uri.host:
|
||||
if not is_hostname_valid(parsed_uri.host):
|
||||
raise DatabaseTestConnectionNetworkError(
|
||||
error_type=SupersetErrorType.TEST_CONNECTION_INVALID_HOSTNAME_ERROR,
|
||||
message=_(
|
||||
'Unable to resolve hostname "%(hostname)s".',
|
||||
hostname=parsed_uri.host,
|
||||
),
|
||||
level=ErrorLevel.ERROR,
|
||||
extra={"hostname": parsed_uri.host},
|
||||
)
|
||||
|
||||
if parsed_uri.port:
|
||||
if not is_port_open(parsed_uri.host, parsed_uri.port):
|
||||
if is_host_up(parsed_uri.host):
|
||||
raise DatabaseTestConnectionNetworkError(
|
||||
error_type=(
|
||||
SupersetErrorType.TEST_CONNECTION_PORT_CLOSED_ERROR
|
||||
),
|
||||
message=_(
|
||||
"The host %(host)s is up, but the port %(port)s is "
|
||||
"closed.",
|
||||
host=parsed_uri.host,
|
||||
port=parsed_uri.port,
|
||||
),
|
||||
level=ErrorLevel.ERROR,
|
||||
extra={
|
||||
"hostname": parsed_uri.host,
|
||||
"port": parsed_uri.port,
|
||||
},
|
||||
)
|
||||
|
||||
raise DatabaseTestConnectionNetworkError(
|
||||
error_type=SupersetErrorType.TEST_CONNECTION_HOST_DOWN_ERROR,
|
||||
message=_(
|
||||
"The host %(host)s might be down, ond can't be reached on "
|
||||
"port %(port)s.",
|
||||
host=parsed_uri.host,
|
||||
port=parsed_uri.port,
|
||||
),
|
||||
level=ErrorLevel.ERROR,
|
||||
extra={"hostname": parsed_uri.host, "port": parsed_uri.port,},
|
||||
)
|
||||
|
||||
def run(self) -> None:
|
||||
self.validate()
|
||||
uri = self._properties.get("sqlalchemy_uri", "")
|
||||
@@ -136,9 +85,9 @@ class TestConnectionDatabaseCommand(BaseCommand):
|
||||
action=f"test_connection_error.{ex.__class__.__name__}",
|
||||
engine=database.db_engine_spec.__name__,
|
||||
)
|
||||
# check if we have connectivity to the host, and if the port is open
|
||||
self._diagnose(uri)
|
||||
raise DatabaseTestConnectionFailedError()
|
||||
# check for custom errors (wrong username, wrong password, etc)
|
||||
errors = database.db_engine_spec.extract_errors(ex)
|
||||
raise DatabaseTestConnectionFailedError(errors)
|
||||
except SupersetSecurityException as ex:
|
||||
event_logger.log_with_context(
|
||||
action=f"test_connection_error.{ex.__class__.__name__}",
|
||||
|
||||
Reference in New Issue
Block a user