Debian Codegraph @

crazy_complete/errors.py

raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]>
3
4 '''This module contains Exception classes for crazy-complete.'''
5
6
7 class CrazyError(Exception):
8 '''Exception class for handling predictable or expected errors.
9
10 This exception is raised in situations where the error condition is
11 anticipated and should be handled gracefully by the program. It is
12 meant to signal issues that are not caused by bugs in the code but
13 by user input.
14 '''
15
16
17 class CrazyTypeError(CrazyError):
18 '''Exception raised for invalid parameter types.
19
20 Args:
21 name (str): The name of the parameter with the invalid type.
22 expected (str): A description of the expected types for the parameter.
23 value (any): The actual value passed that has the wrong type.
24 '''
25
26 def __init__(self, name, expected, value):
27 self.name = name
28 self.expected = expected
29 self.value = value
30 super().__init__(self.__str__())
31
32 def __str__(self):
33 s0 = 'Parameter `%s` has an invalid type.' % self.name
34 s1 = 'Expected types: %s. Received: %r (%s)' % (
35 self.expected, self.value, type(self.value).__name__)
36 return f'{s0} {s1}'
37
38
39 class CrazySchemaValidationError(CrazyError):
40 '''Exception raised for errors in the configuration structure.
41
42 This exception is specifically designed to handle errors encountered when
43 validating YAML or JSON configuration files against a defined schema.
44 It provides detailed error messages, including the line and column numbers
45 where the error occurred, to help locate the issue in the configuration
46 file.
47
48 Args:
49 message (str): The error message.
50 value_with_trace (ValueWithTrace): The value that caused the error.
51 '''
52
53 def __init__(self, message, value_with_trace):
54 self.message = message
55 self.value_with_trace = value_with_trace
56 super().__init__(self.__str__())
57
58 def __str__(self):
59 pos_string = self.value_with_trace.get_position_string()
60
61 if not pos_string:
62 return self.message
63
64 return f'{pos_string}: {self.message}'
65
66
67 class InternalError(Exception):
68 '''Exception raised for internal errors within the program.
69
70 This exception indicates that an unexpected condition has occurred that
71 typically results from a bug or flaw in the code logic. It represents
72 errors that are not caused by user actions or external factors, but
73 rather by mistakes or inconsistencies in the program's internal state.
74 '''