Coverage for pyTooling/Exceptions/__init__.py: 62%
49 statements
« prev ^ index » next coverage.py v7.15.0, created at 2026-07-11 14:13 +0000
« prev ^ index » next coverage.py v7.15.0, created at 2026-07-11 14:13 +0000
1# ==================================================================================================================== #
2# #
3# _____ _ _ _____ _ _ #
4# _ __ _ |_ _|__ ___ | (_)_ __ __ _ | ____|_ _____ ___ _ __ | |_(_) ___ _ __ ___ #
5# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | | _| \ \/ / __/ _ \ '_ \| __| |/ _ \| '_ \/ __| #
6# | |_) | |_| || | (_) | (_) | | | | | | (_| |_| |___ > < (_| __/ |_) | |_| | (_) | | | \__ \ #
7# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)_____/_/\_\___\___| .__/ \__|_|\___/|_| |_|___/ #
8# |_| |___/ |___/ |_| #
9# ==================================================================================================================== #
10# Authors: #
11# Patrick Lehmann #
12# #
13# License: #
14# ==================================================================================================================== #
15# Copyright 2017-2026 Patrick Lehmann - Bötzingen, Germany #
16# #
17# Licensed under the Apache License, Version 2.0 (the "License"); #
18# you may not use this file except in compliance with the License. #
19# You may obtain a copy of the License at #
20# #
21# http://www.apache.org/licenses/LICENSE-2.0 #
22# #
23# Unless required by applicable law or agreed to in writing, software #
24# distributed under the License is distributed on an "AS IS" BASIS, #
25# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
26# See the License for the specific language governing permissions and #
27# limitations under the License. #
28# #
29# SPDX-License-Identifier: Apache-2.0 #
30# ==================================================================================================================== #
31#
32"""
33A common set of missing exceptions in Python.
35.. hint::
37 See :ref:`high-level help <EXECPTION>` for explanations and usage examples.
38"""
39from typing import Tuple, Iterable, Any
41from pyTooling.Decorators import export, readonly
44@export
45class OverloadResolutionError(Exception):
46 """
47 The exception is raised, when no matching overloaded method was found.
49 .. seealso::
51 :func:`@overloadable <pyTooling.MetaClasses.overloadable>`
52 |rarr| Mark a method as *overloadable*.
53 """
55 @readonly
56 def HasNotes(self) -> bool:
57 """
58 Read-only property to return if the warning has attached notes.
60 :returns: True, if the warning has attached notes.
61 """
62 return hasattr(self, "__notes__") and self.__notes__ is not None and len(self.__notes__) > 0
64 @readonly
65 def Notes(self) -> Tuple[str, ...]:
66 """
67 Read-only property to access warning's attached notes.
69 :returns: Attached notes.
70 """
71 return tuple(self.__notes__) if hasattr(self, "__notes__") else tuple()
74@export
75class ExceptionBase(Exception):
76 """Base exception derived from :exc:`Exception <python:Exception>` for all custom exceptions."""
78 def __init__(self, message: str = "") -> None:
79 """
80 ExceptionBase initializer.
82 :param message: The exception message.
83 """
84 super().__init__()
85 self.message = message
87 @readonly
88 def HasNotes(self) -> bool:
89 """
90 Read-only property to return if the warning has attached notes.
92 :returns: True, if the warning has attached notes.
93 """
94 return hasattr(self, "__notes__") and self.__notes__ is not None and len(self.__notes__) > 0
96 @readonly
97 def Notes(self) -> Tuple[str, ...]:
98 """
99 Read-only property to access warning's attached notes.
101 :returns: Attached notes.
102 """
103 return tuple(self.__notes__) if hasattr(self, "__notes__") else tuple()
105 def __str__(self) -> str:
106 """Returns the exception's message text."""
107 return self.message
109 # @DocumentMemberAttribute(False)
110 # @MethodAlias(pyExceptions.with_traceback)
111 # def with_traceback(self): pass
114@export
115class EnvironmentException(ExceptionBase):
116 """The exception is raised when an expected environment variable is missing."""
119@export
120class PlatformNotSupportedException(ExceptionBase):
121 """The exception is raise if the platform is not supported."""
124@export
125class NotConfiguredException(ExceptionBase):
126 """The exception is raise if the requested setting is not configured."""
129@export
130class ToolingException(Exception):
131 """The exception is raised by pyTooling internal features."""
133 @readonly
134 def HasNotes(self) -> bool:
135 """
136 Read-only property to return if the warning has attached notes.
138 :returns: True, if the warning has attached notes.
139 """
140 return hasattr(self, "__notes__") and self.__notes__ is not None and len(self.__notes__) > 0
142 @readonly
143 def Notes(self) -> Tuple[str, ...]:
144 """
145 Read-only property to access warning's attached notes.
147 :returns: Attached notes.
148 """
149 return tuple(self.__notes__) if hasattr(self, "__notes__") else tuple()
152@export
153def addNoteWithItemList(
154 ex: BaseException,
155 message: str,
156 items: Iterable[Any],
157 *,
158 indent: str = " ",
159 separator: str = ", ",
160 maxWidth: int = 100
161) -> None:
162 """
163 Add a message as a note to the exception. The iterables items are added as a coma separated list. If the list gets too
164 long, remaining items will be continued in addition notes.
166 :param ex: Exception to attach the note to.
167 :param message: The message of the note.
168 :param items: An iterable of items to add to the note.
169 :param indent: The indentation of the additional notes.
170 :param separator: Separator between items.
171 :param maxWidth: The maximum width of the attached notes.
172 """
173 note = message
174 sep = ""
176 for item in items:
177 if len(note) + len(newItem := f"{sep}{item}") <= maxWidth:
178 note += newItem
179 sep = separator
180 else:
181 ex.add_note(note)
183 note = f"{indent}{item}"
184 sep = separator
186 ex.add_note(note)