Python · 2238 bytes Raw Blame History
1 """Typed errors raised by `dlm.lock`."""
2
3 from __future__ import annotations
4
5 from pathlib import Path
6
7
8 class LockError(Exception):
9 """Base class for `dlm.lock` failures."""
10
11
12 class LockSchemaError(LockError):
13 """`dlm.lock` is present but unreadable or schema-invalid.
14
15 Distinct from `LockValidationError` — this fires when the on-disk
16 file can't be parsed at all. The validator only runs after parse
17 succeeds.
18 """
19
20 def __init__(self, path: Path, reason: str) -> None:
21 self.path = path
22 self.reason = reason
23 super().__init__(f"{path}: {reason}")
24
25
26 class LockWriteError(LockError):
27 """Programmer error on the write path.
28
29 Distinct from `LockSchemaError` (which means "on-disk file can't
30 be parsed") so callers can tell a write-refusal apart from a
31 read-refusal. Raised by `write_lock` when the caller passes a
32 `DlmLock` with a lock_version the writer doesn't support.
33 """
34
35 def __init__(self, *, path: Path, reason: str) -> None:
36 self.path = path
37 self.reason = reason
38 super().__init__(f"{path}: write refused: {reason}")
39
40
41 class LockValidationError(LockError):
42 """Prior and current lock disagree at `error` severity.
43
44 Carries the list of mismatched fields so the CLI can render a
45 human-actionable report (`--update-lock` / `--ignore-lock` /
46 investigate). Warn-severity mismatches don't raise.
47 """
48
49 def __init__(self, *, path: Path, reasons: list[str]) -> None:
50 self.path = path
51 self.reasons = list(reasons)
52 joined = "; ".join(reasons)
53 super().__init__(f"{path}: lock validation failed ({joined})")
54
55
56 class GoldenIndexSchemaError(LockError):
57 """Repo-level determinism-golden index is unreadable or schema-invalid."""
58
59 def __init__(self, path: Path, reason: str) -> None:
60 self.path = path
61 self.reason = reason
62 super().__init__(f"{path}: {reason}")
63
64
65 class GoldenIndexWriteError(LockError):
66 """Programmer error on the repo-level determinism-golden index write path."""
67
68 def __init__(self, *, path: Path, reason: str) -> None:
69 self.path = path
70 self.reason = reason
71 super().__init__(f"{path}: write refused: {reason}")