Skip to content

Commit

Permalink
fix: handle Edge errors (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
alespour authored Aug 5, 2024
1 parent 70ceac0 commit 9fd9ebc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 0.8.0 [unreleased]

### Bug Fixes

1. [#100](https://github.com/InfluxCommunity/influxdb3-python/pull/100): InfluxDB Edge (OSS) error handling

## 0.7.0 [2024-07-11]

### Bug Fixes
Expand Down
12 changes: 11 additions & 1 deletion influxdb_client_3/write_client/client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ def _get_message(self, response):
# Body
if response.data:
import json

def get(d, key):
if not key or d is None:
return d
return get(d.get(key[0]), key[1:])
try:
return json.loads(response.data)["message"]
node = json.loads(response.data)
for key in [['message'], ['data', 'error_message'], ['error']]:
value = get(node, key)
if value is not None:
return value
return response.data
except Exception as e:
logging.debug(f"Cannot parse error response to JSON: {response.data}, {e}")
return response.data
Expand Down
38 changes: 37 additions & 1 deletion tests/test_api_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import unittest
from unittest import mock
from urllib3 import response

from influxdb_client_3.write_client._sync.api_client import ApiClient
from influxdb_client_3.write_client.configuration import Configuration
from influxdb_client_3.write_client.client.exceptions import InfluxDBError
from influxdb_client_3.write_client.service import WriteService
from influxdb_client_3.version import VERSION


_package = "influxdb3-python"
_sentHeaders = {}

Expand Down Expand Up @@ -69,3 +70,38 @@ def test_call_api(self, mock_post):
self.assertEqual("Bearer TEST_TOKEN", _sentHeaders["Authorization"])
self.assertIsNotNone(_sentHeaders["User-Agent"])
self.assertEqual(f"{_package}/{VERSION}", _sentHeaders["User-Agent"])

def _test_api_error(self, body):
conf = Configuration()
client = ApiClient(conf)
client.rest_client.pool_manager.request \
= mock.Mock(return_value=response.HTTPResponse(status=400,
reason='Bad Request',
body=body.encode()))
service = WriteService(client)
service.post_write("TEST_ORG", "TEST_BUCKET", "data,foo=bar val=3.14")

def test_api_error_cloud(self):
response_body = '{"message": "parsing failed for write_lp endpoint"}'
with self.assertRaises(InfluxDBError) as err:
self._test_api_error(response_body)
self.assertEqual('parsing failed for write_lp endpoint', err.exception.message)

def test_api_error_oss_without_detail(self):
response_body = '{"error": "parsing failed for write_lp endpoint"}'
with self.assertRaises(InfluxDBError) as err:
self._test_api_error(response_body)
self.assertEqual('parsing failed for write_lp endpoint', err.exception.message)

def test_api_error_oss_with_detail(self):
response_body = ('{"error":"parsing failed for write_lp endpoint","data":{"error_message":"invalid field value '
'in line protocol for field \'val\' on line 1"}}')
with self.assertRaises(InfluxDBError) as err:
self._test_api_error(response_body)
self.assertEqual('invalid field value in line protocol for field \'val\' on line 1', err.exception.message)

def test_api_error_unknown(self):
response_body = '{"detail":"no info"}'
with self.assertRaises(InfluxDBError) as err:
self._test_api_error(response_body)
self.assertEqual(response_body, err.exception.message)

0 comments on commit 9fd9ebc

Please sign in to comment.