dosubot[bot] commented on issue #29497:
URL: https://github.com/apache/superset/issues/29497#issuecomment-2210324540
<!-- Greeting -->
@hmily3913 Hello there! I'm a bot here to help you with bug fixes,
questions, and contributions. If you need any assistance, feel free to ask.
<!-- Answer -->
To resolve the error when creating a SQL Server database connection in
Superset, given that the credentials are correct but the connection fails with
a `DBAPIError`, you should check for custom errors extracted by the
`db_engine_spec` of the database. The relevant code snippet shows that the
`DBAPIError` is caught and custom errors are extracted and raised as
`SupersetErrorsException`.
Here is the relevant part of the code:
```python
except DBAPIError as ex:
event_logger.log_with_context(
action=get_log_connection_action(
"test_connection_error", ssh_tunnel, ex
),
engine=database.db_engine_spec.__name__,
)
# check for custom errors (wrong username, wrong password, etc)
errors = database.db_engine_spec.extract_errors(ex, self._context)
raise SupersetErrorsException(errors) from ex
```
To resolve the error, you should:
1. Check the custom errors extracted by
`database.db_engine_spec.extract_errors(ex, self._context)`.
2. Address the specific issues indicated by these custom errors.
Superset defines various types of errors that can occur, including but not
limited to:
- `CONNECTION_INVALID_USERNAME_ERROR`
- `CONNECTION_INVALID_PASSWORD_ERROR`
- `CONNECTION_INVALID_HOSTNAME_ERROR`
- `CONNECTION_PORT_CLOSED_ERROR`
- `CONNECTION_HOST_DOWN_ERROR`
- `CONNECTION_ACCESS_DENIED_ERROR`
- `CONNECTION_UNKNOWN_DATABASE_ERROR`
- `CONNECTION_DATABASE_PERMISSIONS_ERROR`
- `CONNECTION_MISSING_PARAMETERS_ERROR`
- `SYNTAX_ERROR`
- `CONNECTION_DATABASE_TIMEOUT`
For example, if the custom errors indicate issues such as wrong username
(`CONNECTION_INVALID_USERNAME_ERROR`), wrong password
(`CONNECTION_INVALID_PASSWORD_ERROR`), or other connection-related problems,
you will need to correct those specific issues in your connection configuration.
Additionally, Superset provides issue codes and messages for these errors,
which can help you diagnose and resolve the problem more effectively. For
instance:
- Issue code `1012`: "The username provided when connecting to a database is
not valid."
- Issue code `1013`: "The password provided when connecting to a database is
not valid."
- Issue code `1007`: "The hostname provided can't be resolved."
- Issue code `1008`: "The port is closed."
- Issue code `1009`: "The host might be down, and can't be reached on the
provided port."
In the case of SQL Server, the `MssqlEngineSpec` class in Superset defines
specific regular expressions to catch custom errors:
```python
CONNECTION_ACCESS_DENIED_REGEX = re.compile("Adaptive Server connection
failed")
CONNECTION_INVALID_HOSTNAME_REGEX = re.compile(
r"Adaptive Server is unavailable or does not exist \((?P<hostname>.*?)\)"
"(?!.*Net-Lib error).*$"
)
CONNECTION_PORT_CLOSED_REGEX = re.compile(
r"Net-Lib error during Connection refused \(61\)"
)
CONNECTION_HOST_DOWN_REGEX = re.compile(
r"Net-Lib error during Operation timed out \(60\)"
)
```
These regular expressions are used to match specific error messages and map
them to Superset error types:
```python
custom_errors: dict[Pattern[str], tuple[str, SupersetErrorType, dict[str,
Any]]] = {
CONNECTION_ACCESS_DENIED_REGEX: (
__(
'Either the username "%(username)s", password, '
'or database name "%(database)s" is incorrect.'
),
SupersetErrorType.CONNECTION_ACCESS_DENIED_ERROR,
{},
),
CONNECTION_INVALID_HOSTNAME_REGEX: (
__('The hostname "%(hostname)s" cannot be resolved.'),
SupersetErrorType.CONNECTION_INVALID_HOSTNAME_ERROR,
{},
),
CONNECTION_PORT_CLOSED_REGEX: (
__('Port %(port)s on hostname "%(hostname)s" refused the
connection.'),
SupersetErrorType.CONNECTION_PORT_CLOSED_ERROR,
{},
),
CONNECTION_HOST_DOWN_REGEX: (
__(
'The host "%(hostname)s" might be down, and can\'t be '
"reached on port %(port)s."
),
SupersetErrorType.CONNECTION_HOST_DOWN_ERROR,
{},
),
}
```
By addressing the specific issues indicated by these custom errors and issue
codes, you can resolve the `DBAPIError` and successfully create a SQL Server
database connection in Superset
<sup>[[1]](https://github.com/apache/superset/blob/master/superset/commands/database/test_connection.py)</sup><sup>[[2]](https://github.com/apache/superset/blob/master/superset/errors.py)</sup><sup>[[3]](https://github.com/apache/superset/blob/master/superset/db_engine_specs/mssql.py)</sup>.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]