Skip to content

Commit 9fd9ebc

Browse files
authored
fix: handle Edge errors (#100)
1 parent 70ceac0 commit 9fd9ebc

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 0.8.0 [unreleased]
44

5+
### Bug Fixes
6+
7+
1. [#100](https://github.com/InfluxCommunity/influxdb3-python/pull/100): InfluxDB Edge (OSS) error handling
8+
59
## 0.7.0 [2024-07-11]
610

711
### Bug Fixes

influxdb_client_3/write_client/client/exceptions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ def _get_message(self, response):
2626
# Body
2727
if response.data:
2828
import json
29+
30+
def get(d, key):
31+
if not key or d is None:
32+
return d
33+
return get(d.get(key[0]), key[1:])
2934
try:
30-
return json.loads(response.data)["message"]
35+
node = json.loads(response.data)
36+
for key in [['message'], ['data', 'error_message'], ['error']]:
37+
value = get(node, key)
38+
if value is not None:
39+
return value
40+
return response.data
3141
except Exception as e:
3242
logging.debug(f"Cannot parse error response to JSON: {response.data}, {e}")
3343
return response.data

tests/test_api_client.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import unittest
22
from unittest import mock
3+
from urllib3 import response
34

45
from influxdb_client_3.write_client._sync.api_client import ApiClient
56
from influxdb_client_3.write_client.configuration import Configuration
7+
from influxdb_client_3.write_client.client.exceptions import InfluxDBError
68
from influxdb_client_3.write_client.service import WriteService
79
from influxdb_client_3.version import VERSION
810

9-
1011
_package = "influxdb3-python"
1112
_sentHeaders = {}
1213

@@ -69,3 +70,38 @@ def test_call_api(self, mock_post):
6970
self.assertEqual("Bearer TEST_TOKEN", _sentHeaders["Authorization"])
7071
self.assertIsNotNone(_sentHeaders["User-Agent"])
7172
self.assertEqual(f"{_package}/{VERSION}", _sentHeaders["User-Agent"])
73+
74+
def _test_api_error(self, body):
75+
conf = Configuration()
76+
client = ApiClient(conf)
77+
client.rest_client.pool_manager.request \
78+
= mock.Mock(return_value=response.HTTPResponse(status=400,
79+
reason='Bad Request',
80+
body=body.encode()))
81+
service = WriteService(client)
82+
service.post_write("TEST_ORG", "TEST_BUCKET", "data,foo=bar val=3.14")
83+
84+
def test_api_error_cloud(self):
85+
response_body = '{"message": "parsing failed for write_lp endpoint"}'
86+
with self.assertRaises(InfluxDBError) as err:
87+
self._test_api_error(response_body)
88+
self.assertEqual('parsing failed for write_lp endpoint', err.exception.message)
89+
90+
def test_api_error_oss_without_detail(self):
91+
response_body = '{"error": "parsing failed for write_lp endpoint"}'
92+
with self.assertRaises(InfluxDBError) as err:
93+
self._test_api_error(response_body)
94+
self.assertEqual('parsing failed for write_lp endpoint', err.exception.message)
95+
96+
def test_api_error_oss_with_detail(self):
97+
response_body = ('{"error":"parsing failed for write_lp endpoint","data":{"error_message":"invalid field value '
98+
'in line protocol for field \'val\' on line 1"}}')
99+
with self.assertRaises(InfluxDBError) as err:
100+
self._test_api_error(response_body)
101+
self.assertEqual('invalid field value in line protocol for field \'val\' on line 1', err.exception.message)
102+
103+
def test_api_error_unknown(self):
104+
response_body = '{"detail":"no info"}'
105+
with self.assertRaises(InfluxDBError) as err:
106+
self._test_api_error(response_body)
107+
self.assertEqual(response_body, err.exception.message)

0 commit comments

Comments
 (0)