-
Notifications
You must be signed in to change notification settings - Fork 14.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: dbapi exception mapping for dbapi's #12869
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
Optional, | ||
Pattern, | ||
Tuple, | ||
Type, | ||
TYPE_CHECKING, | ||
Union, | ||
) | ||
|
@@ -177,6 +178,35 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods | |
), | ||
} | ||
|
||
@classmethod | ||
def get_dbapi_exception_mapping(cls) -> Dict[Type[Exception], Type[Exception]]: | ||
""" | ||
Each engine can implement and converge it's own specific exceptions into | ||
SQLAlchemy DBAPI exceptions | ||
|
||
Note: On python 3.9 this method can be changed to a classmethod property | ||
without the need of implementing a metaclass type | ||
|
||
:return: A map of driver specific exception to superset custom exceptions | ||
""" | ||
return {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dpgaspar you should be able to build this automatically, since the SQLAlchemy dialect exposts the DB API module, which should have the exceptions at the top level with standard names. Something like this (untested): dbapi = cls.get_engine(database).dialect.dbapi()
return {
getattr(dbapi, name): getattr(superset.exceptions, f"SupersetDBAPI{name}"
for name in {"ProgrammingError", "DatabaseError", etc}
} Maybe have this as the base method and allow subclasses to define their own? |
||
|
||
@classmethod | ||
def get_dbapi_mapped_exception(cls, exception: Exception) -> Exception: | ||
""" | ||
Get a superset custom DBAPI exception from the driver specific exception. | ||
|
||
Override if the engine needs to perform extra changes to the exception, for | ||
example change the exception message or implement custom more complex logic | ||
|
||
:param exception: The driver specific exception | ||
:return: Superset custom DBAPI exception | ||
""" | ||
new_exception = cls.get_dbapi_exception_mapping().get(type(exception)) | ||
if not new_exception: | ||
return exception | ||
return new_exception(str(exception)) | ||
|
||
@classmethod | ||
def is_db_column_type_match( | ||
cls, db_column_type: Optional[str], target_column_type: utils.GenericDataType | ||
|
@@ -314,9 +344,12 @@ def fetch_data( | |
""" | ||
if cls.arraysize: | ||
cursor.arraysize = cls.arraysize | ||
if cls.limit_method == LimitMethod.FETCH_MANY and limit: | ||
return cursor.fetchmany(limit) | ||
return cursor.fetchall() | ||
try: | ||
if cls.limit_method == LimitMethod.FETCH_MANY and limit: | ||
return cursor.fetchmany(limit) | ||
return cursor.fetchall() | ||
except Exception as ex: | ||
raise cls.get_dbapi_mapped_exception(ex) | ||
|
||
@classmethod | ||
def expand_data( | ||
|
@@ -902,7 +935,10 @@ def execute(cls, cursor: Any, query: str, **kwargs: Any) -> None: | |
""" | ||
if cls.arraysize: | ||
cursor.arraysize = cls.arraysize | ||
cursor.execute(query) | ||
try: | ||
cursor.execute(query) | ||
except Exception as ex: | ||
raise cls.get_dbapi_mapped_exception(ex) | ||
|
||
@classmethod | ||
def make_label_compatible(cls, label: str) -> Union[str, quoted_name]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from superset.exceptions import SupersetException | ||
|
||
|
||
class SupersetDBAPIError(SupersetException): | ||
pass | ||
|
||
|
||
class SupersetDBAPIDataError(SupersetDBAPIError): | ||
pass | ||
|
||
|
||
class SupersetDBAPIDatabaseError(SupersetDBAPIError): | ||
pass | ||
|
||
|
||
class SupersetDBAPIDisconnectionError(SupersetDBAPIError): | ||
pass | ||
|
||
|
||
class SupersetDBAPIOperationalError(SupersetDBAPIError): | ||
pass | ||
|
||
|
||
class SupersetDBAPIProgrammingError(SupersetDBAPIError): | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar nit: "its" not "it's"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, yes that's a good point, a simple way of doing is:
But can have broader implications, I want to place the base for this, and just interfere on the defined exceptions for Elasticsearch and clickhouse (for now).