Skip to content

Commit b0794da

Browse files
feat: improve error handling
1 parent e3f8f9d commit b0794da

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

Examples/handle_query_error.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Demonstrates handling error when querying InfluxDB.
3+
"""
4+
import logging
5+
from config import Config
6+
from influxdb_client_3.influxdb_client_error import InfluxdbClientQueryError
7+
8+
import influxdb_client_3 as InfluxDBClient3
9+
10+
11+
def main() -> None:
12+
"""
13+
Main function
14+
:return:
15+
"""
16+
config = Config()
17+
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
18+
19+
client = InfluxDBClient3.InfluxDBClient3(
20+
host=config.host,
21+
token=config.token,
22+
database=config.database
23+
)
24+
25+
try:
26+
# Select from a bucket that doesn't exist
27+
client.query("Select a from cpu11")
28+
except InfluxdbClientQueryError as e:
29+
logging.log(logging.ERROR, e.message)
30+
31+
32+
if __name__ == "__main__":
33+
main()

influxdb_client_3/influxdb_client_error.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
class InfluxDBClientError(Exception):
2+
"""
3+
Exception raised for errors in the InfluxDB client operations.
4+
5+
Represents errors that occur during interactions with the InfluxDB
6+
database client. This exception is a general base class for more
7+
specific client-related failures and is typically used to signal issues
8+
such as invalid queries, connection failures, or API misusage.
9+
"""
210
pass
311

412

513
class InfluxdbClientQueryError(InfluxDBClientError):
14+
"""
15+
Represents an error that occurs when querying an InfluxDB client.
16+
17+
This class is specifically designed to handle errors originating from
18+
client queries to an InfluxDB database. It extends the general
19+
`InfluxDBClientError`, allowing more precise identification and
20+
handling of query-related issues.
21+
22+
:ivar message: Contains the specific error message describing the
23+
query error.
24+
:type message: str
25+
"""
626
def __init__(self, error_message, *args, **kwargs):
727
super().__init__(error_message, *args, **kwargs)
828
self.message = error_message
9-
10-
11-
class InfluxdbClientWriteError(InfluxDBClientError):
12-
pass

0 commit comments

Comments
 (0)