Skip to content

Commit

Permalink
Refactor Base Exceptions (#8989)
Browse files Browse the repository at this point in the history
* moving types_pb2.py to common/events

* Refactor Base Exceptions

* update make_log_dir_if_missing to handle str

* move remaining adapters exception imports to common/adapters
---------

Co-authored-by: Michelle Ark <[email protected]>
  • Loading branch information
colin-rogers-dbt and MichelleArk authored Nov 10, 2023
1 parent 4ee9504 commit 51b94b2
Show file tree
Hide file tree
Showing 131 changed files with 1,140 additions and 1,110 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20231107-135635.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Remove legacy logger
time: 2023-11-07T13:56:35.186648-08:00
custom:
Author: colin-rogers-dbt
Issue: "8027"
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20231107-135728.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Remove use of dbt/core exceptions in dbt/adapter
time: 2023-11-07T13:57:28.683727-08:00
custom:
Author: colin-rogers-dbt MichelleArk
Issue: "8920"
2 changes: 1 addition & 1 deletion core/dbt/adapters/base/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from typing import Dict, ClassVar, Any, Optional

from dbt.exceptions import DbtRuntimeError
from dbt.common.exceptions import DbtRuntimeError


@dataclass
Expand Down
45 changes: 28 additions & 17 deletions core/dbt/adapters/base/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

import agate

import dbt.exceptions
import dbt.adapters.exceptions
import dbt.common.exceptions.base
from dbt.adapters.contracts.connection import (
Connection,
Identifier,
Expand Down Expand Up @@ -91,13 +92,15 @@ def get_thread_connection(self) -> Connection:
key = self.get_thread_identifier()
with self.lock:
if key not in self.thread_connections:
raise dbt.exceptions.InvalidConnectionError(key, list(self.thread_connections))
raise dbt.adapters.exceptions.InvalidConnectionError(
key, list(self.thread_connections)
)
return self.thread_connections[key]

def set_thread_connection(self, conn: Connection) -> None:
key = self.get_thread_identifier()
if key in self.thread_connections:
raise dbt.exceptions.DbtInternalError(
raise dbt.common.exceptions.DbtInternalError(

Check warning on line 103 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L103

Added line #L103 was not covered by tests
"In set_thread_connection, existing connection exists for {}"
)
self.thread_connections[key] = conn
Expand Down Expand Up @@ -137,7 +140,7 @@ def exception_handler(self, sql: str) -> ContextManager:
:return: A context manager that handles exceptions raised by the
underlying database.
"""
raise dbt.exceptions.NotImplementedError(
raise dbt.common.exceptions.base.NotImplementedError(

Check warning on line 143 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L143

Added line #L143 was not covered by tests
"`exception_handler` is not implemented for this adapter!"
)

Expand Down Expand Up @@ -220,22 +223,22 @@ def retry_connection(
:param int _attempts: Parameter used to keep track of the number of attempts in calling the
connect function across recursive calls. Passed as an argument to retry_timeout if it
is a Callable. This parameter should not be set by the initial caller.
:raises dbt.exceptions.FailedToConnectError: Upon exhausting all retry attempts without
:raises dbt.adapters.exceptions.FailedToConnectError: Upon exhausting all retry attempts without
successfully acquiring a handle.
:return: The given connection with its appropriate state and handle attributes set
depending on whether we successfully acquired a handle or not.
"""
timeout = retry_timeout(_attempts) if callable(retry_timeout) else retry_timeout
if timeout < 0:
raise dbt.exceptions.FailedToConnectError(
raise dbt.adapters.exceptions.FailedToConnectError(
"retry_timeout cannot be negative or return a negative time."
)

if retry_limit < 0 or retry_limit > sys.getrecursionlimit():
# This guard is not perfect others may add to the recursion limit (e.g. built-ins).
connection.handle = None
connection.state = ConnectionState.FAIL
raise dbt.exceptions.FailedToConnectError("retry_limit cannot be negative")
raise dbt.adapters.exceptions.FailedToConnectError("retry_limit cannot be negative")

try:
connection.handle = connect()
Expand All @@ -246,7 +249,7 @@ def retry_connection(
if retry_limit <= 0:
connection.handle = None
connection.state = ConnectionState.FAIL
raise dbt.exceptions.FailedToConnectError(str(e))
raise dbt.adapters.exceptions.FailedToConnectError(str(e))

logger.debug(
f"Got a retryable error when attempting to open a {cls.TYPE} connection.\n"
Expand All @@ -268,12 +271,12 @@ def retry_connection(
except Exception as e:
connection.handle = None
connection.state = ConnectionState.FAIL
raise dbt.exceptions.FailedToConnectError(str(e))
raise dbt.adapters.exceptions.FailedToConnectError(str(e))

@abc.abstractmethod
def cancel_open(self) -> Optional[List[str]]:
"""Cancel all open connections on the adapter. (passable)"""
raise dbt.exceptions.NotImplementedError(
raise dbt.common.exceptions.base.NotImplementedError(

Check warning on line 279 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L279

Added line #L279 was not covered by tests
"`cancel_open` is not implemented for this adapter!"
)

Expand All @@ -288,7 +291,9 @@ def open(cls, connection: Connection) -> Connection:
This should be thread-safe, or hold the lock if necessary. The given
connection should not be in either in_use or available.
"""
raise dbt.exceptions.NotImplementedError("`open` is not implemented for this adapter!")
raise dbt.common.exceptions.base.NotImplementedError(

Check warning on line 294 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L294

Added line #L294 was not covered by tests
"`open` is not implemented for this adapter!"
)

def release(self) -> None:
with self.lock:
Expand Down Expand Up @@ -320,12 +325,16 @@ def cleanup_all(self) -> None:
@abc.abstractmethod
def begin(self) -> None:
"""Begin a transaction. (passable)"""
raise dbt.exceptions.NotImplementedError("`begin` is not implemented for this adapter!")
raise dbt.common.exceptions.base.NotImplementedError(

Check warning on line 328 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L328

Added line #L328 was not covered by tests
"`begin` is not implemented for this adapter!"
)

@abc.abstractmethod
def commit(self) -> None:
"""Commit a transaction. (passable)"""
raise dbt.exceptions.NotImplementedError("`commit` is not implemented for this adapter!")
raise dbt.common.exceptions.base.NotImplementedError(

Check warning on line 335 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L335

Added line #L335 was not covered by tests
"`commit` is not implemented for this adapter!"
)

@classmethod
def _rollback_handle(cls, connection: Connection) -> None:
Expand Down Expand Up @@ -361,7 +370,7 @@ def _close_handle(cls, connection: Connection) -> None:
def _rollback(cls, connection: Connection) -> None:
"""Roll back the given connection."""
if connection.transaction_open is False:
raise dbt.exceptions.DbtInternalError(
raise dbt.common.exceptions.DbtInternalError(

Check warning on line 373 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L373

Added line #L373 was not covered by tests
f"Tried to rollback transaction on connection "
f'"{connection.name}", but it does not have one open!'
)
Expand Down Expand Up @@ -412,7 +421,9 @@ def execute(
:return: A tuple of the query status and results (empty if fetch=False).
:rtype: Tuple[AdapterResponse, agate.Table]
"""
raise dbt.exceptions.NotImplementedError("`execute` is not implemented for this adapter!")
raise dbt.common.exceptions.base.NotImplementedError(

Check warning on line 424 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L424

Added line #L424 was not covered by tests
"`execute` is not implemented for this adapter!"
)

def add_select_query(self, sql: str) -> Tuple[Connection, Any]:
"""
Expand All @@ -422,14 +433,14 @@ def add_select_query(self, sql: str) -> Tuple[Connection, Any]:
See https://github.com/dbt-labs/dbt-core/issues/8396 for more information.
"""
raise dbt.exceptions.NotImplementedError(
raise dbt.common.exceptions.base.NotImplementedError(

Check warning on line 436 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L436

Added line #L436 was not covered by tests
"`add_select_query` is not implemented for this adapter!"
)

@classmethod
def data_type_code_to_name(cls, type_code: Union[int, str]) -> str:
"""Get the string representation of the data type from the type_code."""
# https://peps.python.org/pep-0249/#type-objects
raise dbt.exceptions.NotImplementedError(
raise dbt.common.exceptions.base.NotImplementedError(

Check warning on line 444 in core/dbt/adapters/base/connections.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/connections.py#L444

Added line #L444 was not covered by tests
"`data_type_code_to_name` is not implemented for this adapter!"
)
25 changes: 14 additions & 11 deletions core/dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@
import agate
import pytz

from dbt.exceptions import (
from dbt.adapters.exceptions import (
SnapshotTargetIncompleteError,
SnapshotTargetNotSnapshotTableError,
NullRelationDropAttemptedError,
NullRelationCacheAttemptedError,
RelationReturnedMultipleResultsError,
UnexpectedNonTimestampError,
RenameToNoneAttemptedError,
QuoteConfigTypeError,
)

from dbt.common.exceptions import (
NotImplementedError,
DbtInternalError,
DbtRuntimeError,
DbtValidationError,
UnexpectedNullError,
MacroArgTypeError,
MacroResultError,
NotImplementedError,
NullRelationCacheAttemptedError,
NullRelationDropAttemptedError,
QuoteConfigTypeError,
RelationReturnedMultipleResultsError,
RenameToNoneAttemptedError,
SnapshotTargetIncompleteError,
SnapshotTargetNotSnapshotTableError,
UnexpectedNonTimestampError,
UnexpectedNullError,
)

from dbt.adapters.protocol import AdapterConfig
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/adapters/base/query_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dbt.adapters.contracts.connection import AdapterRequiredConfig, QueryComment
from dbt.contracts.graph.nodes import ResultNode
from dbt.contracts.graph.manifest import Manifest
from dbt.exceptions import DbtRuntimeError
from dbt.common.exceptions import DbtRuntimeError


class NodeWrapper:
Expand Down
13 changes: 5 additions & 8 deletions core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@
Policy,
Path,
)
from dbt.exceptions import (
ApproximateMatchError,
DbtInternalError,
MultipleDatabasesNotAllowedError,
)
from dbt.common.exceptions import DbtInternalError
from dbt.adapters.exceptions import MultipleDatabasesNotAllowedError, ApproximateMatchError
from dbt.node_types import NodeType
from dbt.common.utils import filter_null_values, deep_merge
from dbt.adapters.utils import classproperty

import dbt.exceptions
import dbt.common.exceptions


Self = TypeVar("Self", bound="BaseRelation")
Expand Down Expand Up @@ -101,7 +98,7 @@ def matches(

if not search:
# nothing was passed in
raise dbt.exceptions.DbtRuntimeError(
raise dbt.common.exceptions.DbtRuntimeError(

Check warning on line 101 in core/dbt/adapters/base/relation.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/relation.py#L101

Added line #L101 was not covered by tests
"Tried to match relation, but no search path was passed!"
)

Expand Down Expand Up @@ -387,7 +384,7 @@ class InformationSchema(BaseRelation):

def __post_init__(self):
if not isinstance(self.information_schema_view, (type(None), str)):
raise dbt.exceptions.CompilationError(
raise dbt.common.exceptions.CompilationError(

Check warning on line 387 in core/dbt/adapters/base/relation.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/relation.py#L387

Added line #L387 was not covered by tests
"Got an invalid name: {}".format(self.information_schema_view)
)

Expand Down
6 changes: 3 additions & 3 deletions core/dbt/adapters/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
_make_ref_key_dict,
_ReferenceKey,
)
from dbt.exceptions import (
DependentLinkNotCachedError,
from dbt.common.exceptions.cache import (
NewNameAlreadyInCacheError,
NoneRelationFoundError,
ReferencedLinkNotCachedError,
DependentLinkNotCachedError,
TruncatedModelNameCausedCollisionError,
NoneRelationFoundError,
)
from dbt.common.events.functions import fire_event, fire_event_if
from dbt.common.events.types import CacheAction, CacheDumpGraph
Expand Down
4 changes: 4 additions & 0 deletions core/dbt/adapters/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from dbt.adapters.exceptions.compilation import * # noqa
from dbt.adapters.exceptions.alias import * # noqa
from dbt.adapters.exceptions.database import * # noqa
from dbt.adapters.exceptions.connection import * # noqa
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Mapping, Any

from dbt.exceptions import DbtValidationError
from dbt.common.exceptions import DbtValidationError


class AliasError(DbtValidationError):
Expand Down
Loading

0 comments on commit 51b94b2

Please sign in to comment.