basstler-core 4.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
basstler/exceptions.py ADDED
@@ -0,0 +1,639 @@
1
+ """
2
+ Krrood's ``DataclassException`` and the errors built on it.
3
+
4
+ ``DataclassException`` is copied from krrood :cite:`krrood` until krrood's light core is
5
+ released.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from abc import ABC, abstractmethod
11
+ from dataclasses import dataclass
12
+ from enum import StrEnum
13
+ from pathlib import Path, PurePosixPath
14
+
15
+ from basstler.class_property import classproperty
16
+ from basstler.exit_codes import CommandFailure, StackExitCode
17
+ from basstler.locations import BasstlerVariable, ClaudeCodeVariable, PluginLocation
18
+
19
+
20
+ class DataclassException(Exception, ABC):
21
+ """
22
+ A base exception class for dataclass-based exceptions.
23
+
24
+ Subclasses implement error_message() and suggest_correction(); both are evaluated at
25
+ construction time and composed into the actual exception message (the args passed to
26
+ Exception). A non-empty suggest_correction() is rendered as a trailing "Suggestion:
27
+ ..." line. Subclasses that override __post_init__ must call super().__post_init__()
28
+ at the end.
29
+ """
30
+
31
+ def __post_init__(self) -> None:
32
+ # BaseException.__new__ bypasses the usual ABC instantiation check, so enforce it here.
33
+ if getattr(type(self), "__abstractmethods__", None):
34
+ raise TypeError(
35
+ f"Can't instantiate abstract class {type(self).__name__} without an implementation of "
36
+ f"{', '.join(sorted(type(self).__abstractmethods__))}."
37
+ )
38
+ message = self.error_message()
39
+ correction = self.suggest_correction()
40
+ if correction:
41
+ message = f"{message}\nSuggestion: {correction}"
42
+ super().__init__(message)
43
+
44
+ def __str__(self) -> str:
45
+ # Stdlib mixins like KeyError override __str__ to repr their args; always render the
46
+ # plain message that __post_init__ baked into the args.
47
+ return Exception.__str__(self)
48
+
49
+ @abstractmethod
50
+ def error_message(self) -> str:
51
+ """
52
+ :return: A human-readable description of what went wrong.
53
+ """
54
+
55
+ @abstractmethod
56
+ def suggest_correction(self) -> str:
57
+ """
58
+ :return: Advice on how to fix the error, or an empty string if there is no specific advice.
59
+ """
60
+
61
+
62
+ @dataclass
63
+ class NotesBranchMissingError(DataclassException, RuntimeError):
64
+ """
65
+ Raised when no remote tried holds the personal-notes branch.
66
+ """
67
+
68
+ branch: str
69
+ """
70
+ The personal-notes branch that was looked for.
71
+ """
72
+
73
+ attempted_remotes: tuple[str, ...]
74
+ """
75
+ Every remote it was fetched from, in order.
76
+ """
77
+
78
+ def error_message(self) -> str:
79
+ return (
80
+ f"branch '{self.branch}' doesn't exist yet (tried: "
81
+ f"{', '.join(self.attempted_remotes)})"
82
+ )
83
+
84
+ def suggest_correction(self) -> str:
85
+ return f"run {PluginLocation.PERSONAL_NOTES_BRANCH_CREATION_SCRIPT.path_under_exported_root} first"
86
+
87
+
88
+ @dataclass
89
+ class NotesPushRejectedError(DataclassException, RuntimeError):
90
+ """
91
+ Raised when the remote rejected every push of a write to the personal-notes branch.
92
+ """
93
+
94
+ branch: str
95
+ """
96
+ The personal-notes branch pushed to.
97
+ """
98
+
99
+ attempts: int
100
+ """
101
+ How many pushes were made.
102
+ """
103
+
104
+ def error_message(self) -> str:
105
+ return f"every one of {self.attempts} pushes to '{self.branch}' was rejected"
106
+
107
+ def suggest_correction(self) -> str:
108
+ return "check that this credential may push to the branch, then run it again"
109
+
110
+
111
+ @dataclass
112
+ class UnsafeNotesPathError(DataclassException, ValueError):
113
+ """
114
+ Raised when a path to write on the personal-notes branch could leave it.
115
+ """
116
+
117
+ path: PurePosixPath
118
+ """
119
+ The refused path.
120
+ """
121
+
122
+ def error_message(self) -> str:
123
+ return f"{self.path} would be written outside the personal-notes branch's root"
124
+
125
+ def suggest_correction(self) -> str:
126
+ return "give a path relative to the branch's root, with no '..' component"
127
+
128
+
129
+ @dataclass
130
+ class UnresolvableReferenceError(CommandFailure, DataclassException, LookupError):
131
+ """
132
+ Raised when a git reference a decision depends on names no commit in the checkout.
133
+ """
134
+
135
+ @classproperty
136
+ def exit_code(cls) -> StackExitCode:
137
+ """:return: The status this kind of error ends a command with."""
138
+ return StackExitCode.GIT_COMMAND_FAILED
139
+
140
+ reference: str
141
+ """
142
+ The reference git could not resolve.
143
+ """
144
+
145
+ def error_message(self) -> str:
146
+ return f"git cannot resolve '{self.reference}' to a commit in this checkout"
147
+
148
+ def suggest_correction(self) -> str:
149
+ return (
150
+ "fetch the branch it names from its remote, and check that the branch "
151
+ "still exists there"
152
+ )
153
+
154
+
155
+ @dataclass
156
+ class MissingStatusLineError(DataclassException, ValueError):
157
+ """
158
+ Raised when an item slated for a status correction has no ``status:`` line in its
159
+ plan.yaml block.
160
+ """
161
+
162
+ item_identifier: str
163
+ """
164
+ The id of the item whose block has no ``status:`` line.
165
+ """
166
+
167
+ def error_message(self) -> str:
168
+ return f"item {self.item_identifier!r} has no status: line to correct"
169
+
170
+ def suggest_correction(self) -> str:
171
+ return "give the item a status: line in plan.yaml"
172
+
173
+
174
+ # %% calls to git and GitHub
175
+
176
+
177
+ @dataclass
178
+ class ExternalCallFailed(DataclassException, RuntimeError):
179
+ """
180
+ Base for a call to git or GitHub that the caller depended on and did not get.
181
+
182
+ Both carry what was called, the status it came back with and what it said, and
183
+ differ only in how they name the call.
184
+ """
185
+
186
+ status: int
187
+ """The status the call came back with."""
188
+
189
+ detail: str
190
+ """
191
+ What the far side said about it.
192
+ """
193
+
194
+ @property
195
+ def call(self) -> str:
196
+ """:return: The call that failed, named the way its own caller named it."""
197
+ raise NotImplementedError
198
+
199
+ def error_message(self) -> str:
200
+ return f"{self.call} failed with {self.status}: {self.detail}"
201
+
202
+ def suggest_correction(self) -> str:
203
+ return ""
204
+
205
+
206
+ @dataclass
207
+ class MalformedEmailAddressError(DataclassException, ValueError):
208
+ """
209
+ Raised when text is not an email address in ``local-part@domain`` form.
210
+ """
211
+
212
+ text: str
213
+ """
214
+ The value that could not be parsed.
215
+ """
216
+
217
+ def error_message(self) -> str:
218
+ return f"expected an email address as 'local-part@domain', got {self.text!r}"
219
+
220
+ def suggest_correction(self) -> str:
221
+ return "give the address your commits should carry, e.g. you@example.com"
222
+
223
+
224
+ @dataclass
225
+ class BashNotFoundError(DataclassException, FileNotFoundError):
226
+ """
227
+ Raised when no bash a script can run in is found, the Windows launcher for WSL
228
+ aside.
229
+ """
230
+
231
+ passed_over: list[Path]
232
+ """
233
+ The bash programs found in the Windows directory, which start WSL rather than the
234
+ bash the scripts are written for.
235
+ """
236
+
237
+ def error_message(self) -> str:
238
+ if not self.passed_over:
239
+ return "found no bash to run the plugin's scripts in"
240
+ launchers = ", ".join(str(path) for path in self.passed_over)
241
+ return (
242
+ f"found no bash to run the plugin's scripts in: {launchers} starts WSL, not "
243
+ f"Git Bash"
244
+ )
245
+
246
+ def suggest_correction(self) -> str:
247
+ return (
248
+ f"install Git for Windows, or set {ClaudeCodeVariable.GIT_BASH_PATH} to its "
249
+ f"bash.exe"
250
+ )
251
+
252
+
253
+ @dataclass
254
+ class PluginRootUnsetError(DataclassException, LookupError):
255
+ """
256
+ Raised when no plugin root is exported, so no plugin script can be found.
257
+ """
258
+
259
+ def error_message(self) -> str:
260
+ return (
261
+ f"{BasstlerVariable.PLUGIN_ROOT} is not set, so the plugin cannot be found"
262
+ )
263
+
264
+ def suggest_correction(self) -> str:
265
+ return (
266
+ f"source {PluginLocation.PERSONAL_NOTES_CONFIGURATION_SCRIPT} from the "
267
+ f"plugin's root first, or export {BasstlerVariable.PLUGIN_ROOT} as that root"
268
+ )
269
+
270
+
271
+ @dataclass
272
+ class PluginRootNotADirectoryError(DataclassException, NotADirectoryError):
273
+ """
274
+ Raised when the exported plugin root is not a directory.
275
+ """
276
+
277
+ root: Path
278
+ """
279
+ The plugin root exported.
280
+ """
281
+
282
+ def error_message(self) -> str:
283
+ return f"{BasstlerVariable.PLUGIN_ROOT} names {self.root}, which is not a directory"
284
+
285
+ def suggest_correction(self) -> str:
286
+ return (
287
+ f"export {BasstlerVariable.PLUGIN_ROOT} as the directory holding the "
288
+ f"plugin's {PluginLocation.HOOKS} directory"
289
+ )
290
+
291
+
292
+ # %% budget violations
293
+
294
+
295
+ @dataclass
296
+ class BudgetViolation(DataclassException):
297
+ """
298
+ One way a workflow breaks the Actions budget, carrying what a reader needs to fix
299
+ it.
300
+ """
301
+
302
+ workflow: str
303
+ """
304
+ The file name of the workflow that breaks it.
305
+ """
306
+
307
+ def error_message(self) -> str:
308
+ return f"{self.workflow}: {self.problem()}"
309
+
310
+ @abstractmethod
311
+ def problem(self) -> str:
312
+ """
313
+ :return: What is wrong with the workflow.
314
+ """
315
+
316
+
317
+ @dataclass
318
+ class ScheduledWorkflow(BudgetViolation):
319
+ """
320
+ The workflow runs on a schedule, which nothing in this repository may.
321
+ """
322
+
323
+ def problem(self) -> str:
324
+ return "runs on a schedule"
325
+
326
+ def suggest_correction(self) -> str:
327
+ return "trigger it on the event it is waiting for instead"
328
+
329
+
330
+ @dataclass
331
+ class MissingPullRequestCancellation(BudgetViolation):
332
+ """
333
+ The workflow runs on every push to a pull request without cancelling the run for the
334
+ commit before.
335
+ """
336
+
337
+ def problem(self) -> str:
338
+ return "runs on every pull-request push without cancel-in-progress concurrency"
339
+
340
+ def suggest_correction(self) -> str:
341
+ return "add a concurrency group with cancel-in-progress: true"
342
+
343
+
344
+ @dataclass
345
+ class MissingJobTimeout(BudgetViolation):
346
+ """
347
+ A job that takes a runner declares no ``timeout-minutes``, so a hang holds the
348
+ runner for GitHub's six-hour default.
349
+ """
350
+
351
+ job: str
352
+ """
353
+ The job's key.
354
+ """
355
+
356
+ def problem(self) -> str:
357
+ return f"job {self.job} has no timeout-minutes"
358
+
359
+ def suggest_correction(self) -> str:
360
+ return f"set timeout-minutes on job {self.job}"
361
+
362
+
363
+ @dataclass
364
+ class JobTimeoutOverCeiling(BudgetViolation):
365
+ """
366
+ A job's ``timeout-minutes`` is above the workflow's ceiling.
367
+ """
368
+
369
+ job: str
370
+ """
371
+ The job's key.
372
+ """
373
+
374
+ timeout_minutes: int
375
+ """
376
+ The job's timeout.
377
+ """
378
+
379
+ ceiling_minutes: int
380
+ """
381
+ The workflow's ceiling.
382
+ """
383
+
384
+ def problem(self) -> str:
385
+ return (
386
+ f"job {self.job} times out after {self.timeout_minutes} minutes, "
387
+ f"above the ceiling of {self.ceiling_minutes}"
388
+ )
389
+
390
+ def suggest_correction(self) -> str:
391
+ return (
392
+ f"lower the timeout of job {self.job}, or raise the workflow's ceiling "
393
+ "in the budget file"
394
+ )
395
+
396
+
397
+ @dataclass
398
+ class MissingForkGuard(BudgetViolation):
399
+ """
400
+ A first job of a workflow answering ``push`` does not skip a push to a fork.
401
+ """
402
+
403
+ job: str
404
+ """
405
+ The job's key.
406
+ """
407
+
408
+ condition: str
409
+ """
410
+ The condition the job has to carry.
411
+ """
412
+
413
+ def problem(self) -> str:
414
+ return f"job {self.job} does not skip a push to a fork"
415
+
416
+ def suggest_correction(self) -> str:
417
+ return (
418
+ f"add `if: {self.condition}` to job {self.job}, or run it in one "
419
+ "repository only with `if: github.repository == '<owner>/<name>'`"
420
+ )
421
+
422
+
423
+ @dataclass
424
+ class MissingLabelGuard(BudgetViolation):
425
+ """
426
+ A first job of a workflow answering ``labeled`` runs for any label, without
427
+ comparing the one added.
428
+ """
429
+
430
+ job: str
431
+ """
432
+ The job's key.
433
+ """
434
+
435
+ def problem(self) -> str:
436
+ return f"job {self.job} runs for any label"
437
+
438
+ def suggest_correction(self) -> str:
439
+ return f"compare github.event.label.name in the `if:` of job {self.job}"
440
+
441
+
442
+ @dataclass
443
+ class TooManyJobsOnEveryPush(BudgetViolation):
444
+ """
445
+ A workflow running on every push takes runners for more jobs a run than its ceiling
446
+ allows.
447
+ """
448
+
449
+ jobs: int
450
+ """
451
+ The most jobs one run takes runners for.
452
+ """
453
+
454
+ ceiling: int
455
+ """
456
+ The workflow's ceiling.
457
+ """
458
+
459
+ def problem(self) -> str:
460
+ return (
461
+ f"runs up to {self.jobs} jobs on every push, above the ceiling of "
462
+ f"{self.ceiling}"
463
+ )
464
+
465
+ def suggest_correction(self) -> str:
466
+ return "narrow its triggers or raise its jobs_per_push_run"
467
+
468
+
469
+ @dataclass
470
+ class UncountableJobs(BudgetViolation):
471
+ """
472
+ A workflow running on every push has jobs whose number is only known at run time, so
473
+ its ceiling cannot be checked.
474
+ """
475
+
476
+ def problem(self) -> str:
477
+ return (
478
+ "runs on every push with a matrix computed at run time or a reusable "
479
+ "workflow from another repository, so its jobs cannot be counted"
480
+ )
481
+
482
+ def suggest_correction(self) -> str:
483
+ return "use a literal matrix and reusable workflows from this repository"
484
+
485
+
486
+ @dataclass
487
+ class OverrideWithoutWorkflow(BudgetViolation):
488
+ """
489
+ The budget sets limits for a workflow file the repository does not have.
490
+ """
491
+
492
+ def problem(self) -> str:
493
+ return "has limits in the budget but no workflow file"
494
+
495
+ def suggest_correction(self) -> str:
496
+ return "remove its override"
497
+
498
+
499
+ # %% budget file errors
500
+
501
+
502
+ @dataclass
503
+ class BudgetFileExists(DataclassException, FileExistsError):
504
+ """
505
+ A budget file is already where a new one would be written.
506
+
507
+ Once written, the file is reviewed by hand, so writing over it would bless whatever
508
+ the workflows do now.
509
+ """
510
+
511
+ path: Path
512
+ """
513
+ The existing file.
514
+ """
515
+
516
+ def error_message(self) -> str:
517
+ return f"{self.path} already exists"
518
+
519
+ def suggest_correction(self) -> str:
520
+ return "edit it instead of writing a new one"
521
+
522
+
523
+ @dataclass
524
+ class MissingBudgetFunction(DataclassException, LookupError):
525
+ """
526
+ A budget file does not define the function that returns its budget.
527
+ """
528
+
529
+ path: Path
530
+ """
531
+ The budget file.
532
+ """
533
+
534
+ function: str
535
+ """
536
+ The name of the function it has to define.
537
+ """
538
+
539
+ def error_message(self) -> str:
540
+ return f"{self.path} defines no {self.function}()"
541
+
542
+ def suggest_correction(self) -> str:
543
+ return f"define {self.function}() returning its ActionsBudget"
544
+
545
+
546
+ class PackageMapInconsistency(StrEnum):
547
+ """
548
+ The ways the declared distributions break their layering.
549
+ """
550
+
551
+ DEPENDENCY_ON_A_HIGHER_LAYER = "depends on a distribution in a higher layer"
552
+ """
553
+ The first distribution lists the second, which sits in a higher layer.
554
+ """
555
+
556
+ DEPENDENCY_CYCLE = "depend on each other in a cycle"
557
+ """
558
+ The distributions, in order, each list the next and the last lists the first.
559
+ """
560
+
561
+ UNDECLARED_DISTRIBUTION = "has no declaration"
562
+ """
563
+ The distribution is listed as a dependency, but nothing declares it.
564
+ """
565
+
566
+ MODULE_CLAIMED_TWICE = "both claim the same module"
567
+ """
568
+ Two distributions name the same module or subpackage.
569
+ """
570
+
571
+
572
+ @dataclass
573
+ class InconsistentPackageMap(DataclassException, ValueError):
574
+ """
575
+ The declared distributions are not layered and acyclic.
576
+ """
577
+
578
+ inconsistency: PackageMapInconsistency
579
+ """
580
+ How the declarations break the layering.
581
+ """
582
+
583
+ distributions: tuple[str, ...]
584
+ """
585
+ The package names of the distributions involved.
586
+ """
587
+
588
+ def error_message(self) -> str:
589
+ return f"{', '.join(self.distributions)} {self.inconsistency}"
590
+
591
+ def suggest_correction(self) -> str:
592
+ return "correct the dependencies in the distributions' pyproject.toml files"
593
+
594
+
595
+ @dataclass
596
+ class UndeclaredDistribution(DataclassException, KeyError):
597
+ """
598
+ A declaration was looked up for a distribution the package map does not declare.
599
+ """
600
+
601
+ distribution: str
602
+ """
603
+ The package name of the distribution looked up.
604
+ """
605
+
606
+ def error_message(self) -> str:
607
+ return f"{self.distribution} {PackageMapInconsistency.UNDECLARED_DISTRIBUTION}"
608
+
609
+ def suggest_correction(self) -> str:
610
+ return "add its directory under packages or look up a declared one"
611
+
612
+
613
+ @dataclass
614
+ class DuplicateKeyError(DataclassException, TypeError):
615
+ """
616
+ Raised when two fields of a model read the same key of the object it mirrors.
617
+ """
618
+
619
+ model: str
620
+ """
621
+ The model's name.
622
+ """
623
+
624
+ key: str
625
+ """
626
+ The key both fields read.
627
+ """
628
+
629
+ fields: tuple[str, str]
630
+ """
631
+ The two fields reading it.
632
+ """
633
+
634
+ def error_message(self) -> str:
635
+ first, second = self.fields
636
+ return f"{self.model}.{first} and {self.model}.{second} both read {self.key!r}"
637
+
638
+ def suggest_correction(self) -> str:
639
+ return "Give one of the fields the key it should read instead as its alias."