click-extended 0.2.1__py3-none-any.whl → 0.3.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.
click_extended/errors.py CHANGED
@@ -3,7 +3,8 @@
3
3
  # pylint: disable=too-many-arguments
4
4
  # pylint: disable=too-many-positional-arguments
5
5
 
6
- import typing as t
6
+ import inspect
7
+ from typing import IO, Any
7
8
 
8
9
  import click
9
10
  from click import ClickException
@@ -17,6 +18,7 @@ class ClickExtendedError(Exception):
17
18
  """Base exception for exceptions defined in the `click_extended` library."""
18
19
 
19
20
 
21
+ # Catchable errors
20
22
  class CatchableError(ClickExtendedError):
21
23
  """Base exception for exceptions raised inside a child node.
22
24
 
@@ -34,6 +36,72 @@ class CatchableError(ClickExtendedError):
34
36
  super().__init__(message)
35
37
 
36
38
 
39
+ class ChildNodeProcessError(CatchableError):
40
+ """
41
+ Base class for exceptions which must be raised inside
42
+ the `process()` method of a `ChildNode`.
43
+ """
44
+
45
+ def __init__(self, message: str, **kwargs: str) -> None:
46
+ """
47
+ Initialize a new `ChildNodeProcessError` instance.
48
+
49
+ Args:
50
+ message (str):
51
+ The error message. If child_name is found, it will be
52
+ prefixed automatically. Subclasses can access child_name
53
+ via self.child_name to format custom messages.
54
+
55
+ Raises:
56
+ RuntimeError:
57
+ If raised outside a ChildNode context.
58
+ """
59
+ self.child_name: str | None = None
60
+ frame = inspect.currentframe()
61
+
62
+ if frame:
63
+ current_frame = frame.f_back
64
+ depth = 0
65
+ max_depth = 10
66
+
67
+ while current_frame and depth < max_depth:
68
+ caller_locals = current_frame.f_locals
69
+ if "self" in caller_locals:
70
+ self_obj = caller_locals["self"]
71
+ if hasattr(self_obj, "name") and hasattr(
72
+ self_obj, "process"
73
+ ):
74
+ self.child_name = self_obj.name
75
+ break
76
+ current_frame = current_frame.f_back
77
+ depth += 1
78
+
79
+ if not self.child_name:
80
+ raise RuntimeError(
81
+ f"{self.__class__.__name__} must be raised from within a "
82
+ f"ChildNode.process() method. The exception was raised outside "
83
+ f"a valid ChildNode context."
84
+ )
85
+
86
+ formatted_message = message.format(name=self.child_name, **kwargs)
87
+ super().__init__(formatted_message)
88
+
89
+
90
+ class UnhandledValueError(ChildNodeProcessError):
91
+ """Exception raised when a value in the `process()` method is unexpected."""
92
+
93
+ def __init__(self, value: Any) -> None:
94
+ """
95
+ Initialize a new `UnhandledValueError` instance.
96
+
97
+ Args:
98
+ value (Any):
99
+ The unexpected value.
100
+ """
101
+ message = "Received unexpected value for '{name}' of type '{type}'"
102
+ super().__init__(message, type=type(value).__name__)
103
+
104
+
37
105
  class ValidationError(CatchableError):
38
106
  """Exception raised when validation fails in a child node."""
39
107
 
@@ -42,6 +110,13 @@ class TransformError(CatchableError):
42
110
  """Exception raised when transformation fails in a child node."""
43
111
 
44
112
 
113
+ class UnknownError(ChildNodeProcessError):
114
+ """
115
+ Exception raised when an unexpected error occurs or parts of code
116
+ which is unhandled.
117
+ """
118
+
119
+
45
120
  class ParameterError(ClickException):
46
121
  """Exception raised when parameter validation or transformation fails.
47
122
 
@@ -78,7 +153,7 @@ class ParameterError(ClickException):
78
153
  return f"({self.param_hint}): {self.message}"
79
154
  return self.message
80
155
 
81
- def show(self, file: t.IO[t.Any] | None = None) -> None:
156
+ def show(self, file: IO[Any] | None = None) -> None:
82
157
  """Display the error with usage information (like Click does)."""
83
158
  if file is None:
84
159
  file = get_text_stderr()
@@ -102,6 +177,7 @@ class ParameterError(ClickException):
102
177
  echo(f"Error {self.format_message()}", file=file, color=color)
103
178
 
104
179
 
180
+ # Node errors
105
181
  class NoParentError(ClickExtendedError):
106
182
  """Exception raised when no `ParentNode` has been defined."""
107
183
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: click_extended
3
- Version: 0.2.1
3
+ Version: 0.3.0
4
4
  Summary: An extension to Click with additional features like automatic async support, aliasing and a modular decorator system.
5
5
  Author-email: Marcus Fredriksson <marcus@marcusfredriksson.com>
6
6
  License: MIT License
@@ -0,0 +1,9 @@
1
+ click_extended/__init__.py,sha256=wBUV7-CmgDBKSnDj3_MbYCjHv2zAaATQ795Y9Xs-0NE,858
2
+ click_extended/errors.py,sha256=ITtVGZ1GosrrGnUbUuJQueoJqFCtJvj83dKJrQqkCmU,10500
3
+ click_extended/types.py,sha256=tiz-toTYABwFTsxUMIDRhPiq3H_V3nhrAcV8VGEKyzA,786
4
+ click_extended-0.3.0.dist-info/licenses/AUTHORS.md,sha256=NkShPinjqtnRDQVRyVnfJuOGM56sejauE3WRoYCcbtw,132
5
+ click_extended-0.3.0.dist-info/licenses/LICENSE,sha256=gjO8hzM4mFSBXFikktaXVSgmXGcre91_GPJ-E_yP56E,1075
6
+ click_extended-0.3.0.dist-info/METADATA,sha256=8cIZt72pTFI9IRW-0nO30gwXrSBg_cRrgTmJj0Oba2g,8295
7
+ click_extended-0.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ click_extended-0.3.0.dist-info/top_level.txt,sha256=2G3bm6tCNv80okRm773jKTk-_z1ElY-seaozZrn_TxA,15
9
+ click_extended-0.3.0.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- click_extended/__init__.py,sha256=wBUV7-CmgDBKSnDj3_MbYCjHv2zAaATQ795Y9Xs-0NE,858
2
- click_extended/errors.py,sha256=IQ98vY90enamFr8yHFL6OUeV42tuAYy6NKVYXUsZRhY,8067
3
- click_extended/types.py,sha256=tiz-toTYABwFTsxUMIDRhPiq3H_V3nhrAcV8VGEKyzA,786
4
- click_extended-0.2.1.dist-info/licenses/AUTHORS.md,sha256=NkShPinjqtnRDQVRyVnfJuOGM56sejauE3WRoYCcbtw,132
5
- click_extended-0.2.1.dist-info/licenses/LICENSE,sha256=gjO8hzM4mFSBXFikktaXVSgmXGcre91_GPJ-E_yP56E,1075
6
- click_extended-0.2.1.dist-info/METADATA,sha256=QEedx4AD1rO28TRyfEkoMSeACt3PHwEYVGiLvaP0lPk,8295
7
- click_extended-0.2.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
- click_extended-0.2.1.dist-info/top_level.txt,sha256=2G3bm6tCNv80okRm773jKTk-_z1ElY-seaozZrn_TxA,15
9
- click_extended-0.2.1.dist-info/RECORD,,