11import ast
22from typing import Optional , Union
33
4+ from typing_extensions import TypeAlias
5+
46from dbally .exceptions import DbAllyError
57
8+ IQLNode : TypeAlias = Union [ast .stmt , ast .expr ]
9+
610
711class IQLError (DbAllyError ):
8- """Base exception for all IQL parsing related exceptions."""
12+ """
13+ Base exception for all IQL parsing related exceptions.
14+
15+ Attributes:
16+ node: An IQL Node (AST Exprresion) during which processing an error was encountered.
17+ source: Raw LLM response containing IQL filter calls.
18+ """
19+
20+ node : IQLNode
21+ source : str
922
10- def __init__ (self , message : str , node : Union [ ast . stmt , ast . expr ] , source : str ) -> None :
23+ def __init__ (self , message : str , node : IQLNode , source : str ) -> None :
1124 message = message + ": " + source [node .col_offset : node .end_col_offset ]
1225
1326 super ().__init__ (message )
@@ -18,15 +31,15 @@ def __init__(self, message: str, node: Union[ast.stmt, ast.expr], source: str) -
1831class IQLArgumentParsingError (IQLError ):
1932 """Raised when an argument cannot be parsed into a valid IQL."""
2033
21- def __init__ (self , node : Union [ ast . stmt , ast . expr ] , source : str ) -> None :
34+ def __init__ (self , node : IQLNode , source : str ) -> None :
2235 message = "Not a valid IQL argument"
2336 super ().__init__ (message , node , source )
2437
2538
2639class IQLUnsupportedSyntaxError (IQLError ):
2740 """Raised when trying to parse an unsupported syntax."""
2841
29- def __init__ (self , node : Union [ ast . stmt , ast . expr ] , source : str , context : Optional [str ] = None ) -> None :
42+ def __init__ (self , node : IQLNode , source : str , context : Optional [str ] = None ) -> None :
3043 node_name = node .__class__ .__name__
3144
3245 message = f"{ node_name } syntax is not supported in IQL"
@@ -47,3 +60,24 @@ def __init__(self, node: ast.Name, source: str) -> None:
4760
4861class IQLArgumentValidationError (IQLError ):
4962 """Raised when argument is not valid for a given method."""
63+
64+
65+ class IQLContextNotAllowedError (IQLError ):
66+ """
67+ Raised when a context call/keyword has been passed as an argument to the filter
68+ which does not support contextualization for this specific parameter.
69+ """
70+
71+ def __init__ (self , node : IQLNode , source : str , arg_name : Optional [str ] = None ) -> None :
72+ if arg_name is None :
73+ message = (
74+ "The LLM detected that the context is required to execute the query"
75+ "while the filter signature does not allow it at all."
76+ )
77+ else :
78+ message = (
79+ "The LLM detected that the context is required to execute the query"
80+ f"while the filter signature does allow it for `{ arg_name } ` argument."
81+ )
82+
83+ super ().__init__ (message , node , source )
0 commit comments