1
1
"""Module for interacting with QuestDB."""
2
2
3
3
from types import TracebackType
4
- from typing import Iterable , Type , overload , AsyncGenerator , Any , TypeVar
4
+ from typing import Iterable , Type , overload , AsyncGenerator , Any , TypeVar , Callable
5
5
6
6
import aiohttp
7
7
import pandas as pd
@@ -156,18 +156,27 @@ async def query(self, query_string: str) -> AsyncGenerator[dict[str, Any], None]
156
156
@overload
157
157
async def query (self , query_string : str , into_type : Type [T ]) -> AsyncGenerator [T , None ]: ...
158
158
159
- async def query (self , query_string : str , into_type : Type [T ] | None = None ) -> AsyncGenerator [T , None ]:
159
+ async def query (
160
+ self , query_string : str , into_type : Type [T ] | None = None , error_handler : Callable [[bytes ], None ] | None = None
161
+ ) -> AsyncGenerator [T , None ]:
160
162
"""
161
163
Perform an asynchronous query and yield the results.
162
164
163
165
Args:
164
166
query_string: The SQL query string to execute.
165
167
into_type: Optional type to cast each row into.
168
+ error_handler: Optional function to handle errors in the response.
166
169
167
170
Yields:
168
171
Parsed rows as either dictionaries or instances of the specified type.
169
172
"""
170
- data = self .parse_query_response (await self ._query (query_string ))
173
+ query_response = await self ._query (query_string )
174
+ try :
175
+ data = self .parse_query_response (query_response )
176
+ except Exception as e :
177
+ if error_handler :
178
+ error_handler (query_response )
179
+ raise e
171
180
172
181
for row in self .parse_and_yield_query_response (data , into_type ):
173
182
yield row
@@ -178,18 +187,26 @@ def query_sync(self, query_string: str) -> Iterable[dict[str, Any]]: ...
178
187
@overload
179
188
def query_sync (self , query_string : str , into_type : Type [T ]) -> Iterable [T ]: ...
180
189
181
- def query_sync (self , query_string : str , into_type : Type [T ] | None = None ) -> Iterable [T ] | Iterable [dict ]:
190
+ def query_sync (
191
+ self , query_string : str , into_type : Type [T ] | None = None , error_handler : Callable [[bytes ], None ] | None = None
192
+ ) -> Iterable [T ] | Iterable [dict ]:
182
193
"""
183
194
Perform a synchronous query and return an iterable of results.
184
195
185
196
Args:
186
197
query_string: The SQL query string to execute.
187
198
into_type: Optional type to cast each row into.
188
-
199
+ error_handler: Optional function to handle errors in the response.
189
200
Returns:
190
201
An iterable of parsed rows as either dictionaries or instances of the specified type.
191
202
"""
192
- data = self .parse_query_response (self ._query_sync (query_string ))
203
+ query_response = self ._query_sync (query_string )
204
+ try :
205
+ data = self .parse_query_response (query_response )
206
+ except Exception as e :
207
+ if error_handler :
208
+ error_handler (query_response )
209
+ raise e
193
210
194
211
return self .parse_and_yield_query_response (data , into_type )
195
212
0 commit comments