1
1
import ast
2
2
from typing import Optional , Union
3
3
4
+ from typing_extensions import TypeAlias
5
+
4
6
from dbally .exceptions import DbAllyError
5
7
8
+ IQLNode : TypeAlias = Union [ast .stmt , ast .expr ]
9
+
6
10
7
11
class 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
9
22
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 :
11
24
message = message + ": " + source [node .col_offset : node .end_col_offset ]
12
25
13
26
super ().__init__ (message )
@@ -18,15 +31,15 @@ def __init__(self, message: str, node: Union[ast.stmt, ast.expr], source: str) -
18
31
class IQLArgumentParsingError (IQLError ):
19
32
"""Raised when an argument cannot be parsed into a valid IQL."""
20
33
21
- def __init__ (self , node : Union [ ast . stmt , ast . expr ] , source : str ) -> None :
34
+ def __init__ (self , node : IQLNode , source : str ) -> None :
22
35
message = "Not a valid IQL argument"
23
36
super ().__init__ (message , node , source )
24
37
25
38
26
39
class IQLUnsupportedSyntaxError (IQLError ):
27
40
"""Raised when trying to parse an unsupported syntax."""
28
41
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 :
30
43
node_name = node .__class__ .__name__
31
44
32
45
message = f"{ node_name } syntax is not supported in IQL"
@@ -47,3 +60,24 @@ def __init__(self, node: ast.Name, source: str) -> None:
47
60
48
61
class IQLArgumentValidationError (IQLError ):
49
62
"""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