rusackas commented on code in PR #36856:
URL: https://github.com/apache/superset/pull/36856#discussion_r3553876048
##########
superset/db_engine_specs/snowflake.py:
##########
@@ -44,12 +46,43 @@
from superset.db_engine_specs.postgres import PostgresBaseEngineSpec
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.models.sql_lab import Query
+from superset.superset_typing import (
+ OAuth2ClientConfig,
+ OAuth2State,
+)
from superset.utils import json
from superset.utils.core import get_user_agent, QuerySource
+from superset.utils.oauth2 import encode_oauth2_state, generate_code_challenge
if TYPE_CHECKING:
from superset.models.core import Database
+try:
+ from snowflake.connector.errors import DatabaseError
+except ImportError:
+ # Use a distinct sentinel type when snowflake is not installed to avoid
+ # matching unrelated exception types (using `Exception` would be too
broad).
+ class _SnowflakeDatabaseError(Exception):
+ """Sentinel type to stand in for
snowflake.connector.errors.DatabaseError."""
+
+ pass
+
+ DatabaseError = _SnowflakeDatabaseError
+
+
+class CustomSnowflakeAuthErrorMeta(type):
+ def __instancecheck__(cls, instance: object) -> bool:
+ return (
+ isinstance(instance, SqlalchemyDatabaseError)
+ and isinstance(cast(SqlalchemyDatabaseError, instance).orig,
DatabaseError)
+ and "Invalid OAuth access token" in str(instance)
+ )
+
+
+class CustomSnowflakeAuthError(DatabaseError,
metaclass=CustomSnowflakeAuthErrorMeta):
+ pass
Review Comment:
Optional, not a blocker: a docstring here documents the matcher and flags
that it only fires via `isinstance` (the `needs_oauth2` path) since `except`
clauses don't honor `__instancecheck__`. Doubles as a fix for the CodeAnt
docstring nits on this block. :)
```suggestion
class CustomSnowflakeAuthErrorMeta(type):
"""
Metaclass whose ``__instancecheck__`` matches Snowflake's invalid/expired
OAuth access-token error, so ``CustomSnowflakeAuthError`` can be used as
the
``oauth2_exception`` that triggers the OAuth2 re-auth dance.
This is only honored via ``isinstance()`` (the path used by
``BaseEngineSpec.needs_oauth2()``); ``except`` clauses do not call
``__instancecheck__``, so it must not be relied on for exception
catching.
"""
def __instancecheck__(cls, instance: object) -> bool:
return (
isinstance(instance, SqlalchemyDatabaseError)
and isinstance(cast(SqlalchemyDatabaseError, instance).orig,
DatabaseError)
and "Invalid OAuth access token" in str(instance)
)
class CustomSnowflakeAuthError(DatabaseError,
metaclass=CustomSnowflakeAuthErrorMeta):
"""Snowflake OAuth error type matched via the metaclass above (see note
there)."""
```
--
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]