-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an `error` method to the `Extractor` class that starts tracking of an error. This can be used in one of two ways, either by manually starting and stopping the error state: ``` python e = extractor.error(...) # handle error e.finish() ``` or by using it as a context ``` python with extractor.error(...): # Handle error ``` You can create an instant error (with no duration) by using the `instant()` method: ``` python extractor.error(...).instant() ``` Tracking of start/end times, generating and keeping track of external IDs, reporting in checkins, and so on is all handled automatically.
- Loading branch information
Showing
4 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import typing | ||
from enum import Enum | ||
from types import TracebackType | ||
from uuid import uuid4 | ||
|
||
from cognite.extractorutils.unstable.core.tasks import Task | ||
from cognite.extractorutils.util import now | ||
|
||
if typing.TYPE_CHECKING: | ||
from .base import Extractor | ||
|
||
|
||
class ErrorLevel(Enum): | ||
warning = "warning" | ||
error = "error" | ||
fatal = "fatal" | ||
|
||
|
||
class Error: | ||
def __init__( | ||
self, | ||
level: ErrorLevel, | ||
description: str, | ||
details: str | None, | ||
task: Task | None, | ||
extractor: "Extractor", | ||
) -> None: | ||
self.level = level | ||
self.description = description | ||
self.details = details | ||
|
||
self.external_id = uuid4().hex | ||
self.start_time = now() | ||
self.end_time: int | None = None | ||
|
||
self._extractor = extractor | ||
self._task = task | ||
|
||
self._extractor._report_error(self) | ||
|
||
def instant(self) -> None: | ||
# Only end the error once | ||
if self.end_time is not None: | ||
return | ||
|
||
self.end_time = self.start_time | ||
|
||
# Re-add in case the error has already been reported and dict cleared | ||
self._extractor._report_error(self) | ||
|
||
def finish(self) -> None: | ||
# Only end the error once | ||
if self.end_time is not None: | ||
return | ||
|
||
self.end_time = now() | ||
|
||
# Re-add in case the error has already been reported and dict cleared | ||
self._extractor._report_error(self) | ||
|
||
def __enter__(self) -> "Error": | ||
return self | ||
|
||
def __exit__( | ||
self, | ||
exc_type: typing.Type[BaseException] | None, | ||
exc_val: BaseException | None, | ||
exc_tb: TracebackType | None, | ||
) -> bool: | ||
self.finish() | ||
return exc_val is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters