This repository was archived by the owner on Oct 29, 2024. It is now read-only.
File tree 3 files changed +29
-5
lines changed 3 files changed +29
-5
lines changed Original file line number Diff line number Diff line change @@ -267,18 +267,27 @@ def query(self,
267
267
query ,
268
268
params = {},
269
269
expected_response_code = 200 ,
270
- database = None ):
270
+ database = None ,
271
+ raise_errors = True ):
271
272
"""Send a query to InfluxDB.
272
273
273
274
:param query: the actual query string
274
275
:type query: str
276
+
275
277
:param params: additional parameters for the request, defaults to {}
276
278
:type params: dict
279
+
277
280
:param expected_response_code: the expected status code of response,
278
281
defaults to 200
279
282
:type expected_response_code: int
283
+
280
284
:param database: database to query, defaults to None
281
285
:type database: str
286
+
287
+ :param raise_errors: Whether or not to raise exceptions when InfluxDB
288
+ returns errors, defaults to True
289
+ :type raise_errors: bool
290
+
282
291
:returns: the queried data
283
292
:rtype: :class:`~.ResultSet`
284
293
"""
@@ -295,7 +304,11 @@ def query(self,
295
304
296
305
data = response .json ()
297
306
298
- results = [ResultSet (result ) for result in data .get ('results' , [])]
307
+ results = [
308
+ ResultSet (result , raise_errors = raise_errors )
309
+ for result
310
+ in data .get ('results' , [])
311
+ ]
299
312
300
313
# TODO(aviau): Always return a list. (This would be a breaking change)
301
314
if len (results ) == 1 :
Original file line number Diff line number Diff line change 10
10
class ResultSet (object ):
11
11
"""A wrapper around a single InfluxDB query result"""
12
12
13
- def __init__ (self , series ):
13
+ def __init__ (self , series , raise_errors = True ):
14
14
self ._raw = series
15
+ self ._error = self .raw .get ('error' , None )
15
16
16
- if ' error' in self . raw :
17
- raise InfluxDBClientError (self .raw [ ' error' ] )
17
+ if self . error is not None and raise_errors is True :
18
+ raise InfluxDBClientError (self .error )
18
19
19
20
@property
20
21
def raw (self ):
@@ -25,6 +26,11 @@ def raw(self):
25
26
def raw (self , value ):
26
27
self ._raw = value
27
28
29
+ @property
30
+ def error (self ):
31
+ """Error returned by InfluxDB"""
32
+ return self ._error
33
+
28
34
def __getitem__ (self , key ):
29
35
"""
30
36
:param key: Either a serie name, or a tags_dict, or
Original file line number Diff line number Diff line change @@ -370,6 +370,11 @@ def test_query_fail(self):
370
370
self .assertIn ('database not found: db' ,
371
371
ctx .exception .content )
372
372
373
+ def test_query_fail_ignore_errors (self ):
374
+ result = self .cli .query ('select column_one from foo' ,
375
+ raise_errors = False )
376
+ self .assertEqual (result .error , 'database not found: db' )
377
+
373
378
def test_create_user (self ):
374
379
self .cli .create_user ('test_user' , 'secret_password' )
375
380
rsp = list (self .cli .query ("SHOW USERS" )['results' ])
You can’t perform that action at this time.
0 commit comments