Skip to content

Commit

Permalink
feat(fixers): capture exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
guilatrova committed Sep 12, 2021
1 parent dc3a070 commit ee708a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/tryceratops/fixers/base.py
Expand Up @@ -5,6 +5,8 @@
from tryceratops.processors import Processor
from tryceratops.violations import Violation

from .exceptions import FixerFixException

ViolationType = TypeVar("ViolationType", bound=Violation)
GroupedViolations = dict[str, List[ViolationType]]

Expand Down Expand Up @@ -48,11 +50,14 @@ def _group_violations_by_filename(self, violations: List[Violation]) -> GroupedV
def _process_group(self, filename: str, violations: List[ViolationType]):
with FileFixerHandler(filename) as file:
for violation in violations:
file_lines = file.read_lines()
resulting_lines = self.perform_fix(file_lines, violation)
file.write_fix(resulting_lines)

self.fixes_made += 1
try:
file_lines = file.read_lines()
resulting_lines = self.perform_fix(file_lines, violation)
file.write_fix(resulting_lines)
except Exception as ex:
raise FixerFixException(violation, filename) from ex
else:
self.fixes_made += 1

@abstractmethod
def perform_fix(self, lines: List[str], violation: ViolationType) -> List[str]:
Expand Down
12 changes: 12 additions & 0 deletions src/tryceratops/fixers/exceptions.py
@@ -0,0 +1,12 @@
from tryceratops.violations import Violation


class FixerException(Exception):
pass


class FixerFixException(FixerException):
def __init__(self, violation: Violation, filename: str):
self.violation = violation
self.filename = filename
super().__init__(f"Attempt to fix violation {violation} on file {filename} failed")

0 comments on commit ee708a4

Please sign in to comment.