# Copyright (C) 2018-'24 Frank Sachsenheim## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published# by the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <https://www.gnu.org/licenses/>.""" These are the specific delb exceptions. """from__future__importannotationsfromtypingimportOptional,TYPE_CHECKING,AnyifTYPE_CHECKING:from_delb.typingimportLoader
[docs]classAmbiguousTreeError(DelbBaseException):""" Raised when a single node shall be fetched or created by an XPath expression in a tree where the target position can't be clearly determined. """def__init__(self,message:str):super().__init__(message)
[docs]classFailedDocumentLoading(DelbBaseException):def__init__(self,source:Any,excuses:dict[Loader,str|Exception]):self.source=sourceself.excuses=excusesdef__str__(self):returnf"Couldn't load {self.source!r} with these loaders: {self.excuses}"
[docs]classInvalidCodePath(DelbBaseException,RuntimeError):"""Raised when a code path that is not expected to be executed is reached."""def__init__(self):# pragma: no coversuper().__init__("An unintended path was taken through the code. Please report this bug.")
[docs]classInvalidOperation(DelbBaseException):"""Raised when an invalid operation is attempted by the client code."""pass
[docs]classXPathParsingError(DelbBaseException):"""Raised when an XPath expression can't be parsed."""def__init__(self,expression:Optional[str]=None,position:Optional[int]=None,message:Optional[str]=None,):self.expression=expressionself.position=positionself.message=messagedef__str__(self):expression=self.expressionassertexpressionisnotNoneassertself.messageisnotNoneposition=self.positionassertself.positionisnotNoneexpression_length=len(expression)snippet_end=min(position+16,expression_length)ifexpression_length>snippet_end:snippet=f"`{expression[position:snippet_end]}…`"else:snippet=f"`{expression[position:snippet_end]}`"iflen(snippet)>2:return(f"XPath parsing error at character {position} ({snippet}): "f"{self.message}")else:returnf"XPath parsing error at character {position}: {self.message}"
[docs]classXPathUnsupportedStandardFeature(XPathParsingError):"""Raised when an unsupported XPath expression feature is recognized."""def__init__(self,position:int,feature_description:str):super().__init__(position=position,message=f"{feature_description} is not supported with intention. ""Please consult the documentation regarding the XPath implementation.",)