Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/crawlee/statistics/_error_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ def get_most_common_errors(self, n: int = 3) -> list[tuple[str | None, int]]:
)
return all_errors.most_common(n)

def reset(self) -> None:
"""Forget the tracked errors, keeping the options of the tracker."""
self._errors.clear()

def _get_error_repr(self, file_and_line: str | None, name: str | None, message: str | None) -> str:
"""Get the most specific error representation."""
file_and_line_part = f'{file_and_line}:' if file_and_line else ''
Expand Down
4 changes: 2 additions & 2 deletions src/crawlee/statistics/_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ def calculate(self) -> FinalStatistics:
async def reset(self) -> None:
"""Reset the statistics to their defaults and remove any persistent state."""
await self._state.reset()
self.error_tracker = ErrorTracker()
self.error_tracker_retry = ErrorTracker()
self.error_tracker.reset()
self.error_tracker_retry.reset()
self._requests_in_progress.clear()

def _log(self) -> None:
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/_statistics/test_error_tracker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import traceback
from unittest.mock import AsyncMock, Mock

import pytest

from crawlee.statistics import Statistics
from crawlee.statistics._error_snapshotter import ErrorSnapshotter
from crawlee.statistics._error_tracker import ErrorTracker


Expand Down Expand Up @@ -127,3 +130,31 @@ async def test_error_tracker_error_without_traceback() -> None:
assert error_tracker.unique_error_count == 1
# With no traceback there is no file and line, so the group has just the name and message.
assert error_tracker.get_most_common_errors()[0][0] == 'ValueError:Some value error'


async def test_reset_keeps_options() -> None:
"""Resetting the tracker forgets the errors and keeps its options."""
error_tracker = ErrorTracker(show_error_name=False, save_error_snapshots=True)
await error_tracker.add(ValueError('Some value error'))

error_tracker.reset()

assert error_tracker.total == 0
assert error_tracker.show_error_name is False
assert error_tracker.error_snapshotter is not None


async def test_statistics_reset_keeps_error_snapshots(monkeypatch: pytest.MonkeyPatch) -> None:
"""Resetting the statistics forgets the errors and keeps saving error snapshots."""
capture_snapshot = AsyncMock()
monkeypatch.setattr(ErrorSnapshotter, 'capture_snapshot', capture_snapshot)
statistics = Statistics.with_default_state(save_error_snapshots=True)
error_tracker = statistics.error_tracker
await error_tracker.add(ValueError('Some value error'), context=Mock())

await statistics.reset()
await error_tracker.add(ValueError('Some value error'), context=Mock())

assert statistics.error_tracker is error_tracker
assert error_tracker.total == 1
assert capture_snapshot.await_count == 2
Loading