Skip to content

chore: improve error message when the client cannot find org by name #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ This release introduces a support for new version of InfluxDB OSS API definition
### API
1. [#399](https://github.com/influxdata/influxdb-client-python/pull/399): Use the latest InfluxDB OSS API definitions to generated APIs

### Bug Fixes
1. [#408](https://github.com/influxdata/influxdb-client-python/pull/408): Improve error message when the client cannot find organization by name

## 1.25.0 [2022-01-20]

### Features
Expand Down
4 changes: 2 additions & 2 deletions influxdb_client/client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
class InfluxDBError(Exception):
"""Raised when a server error occurs."""

def __init__(self, response: HTTPResponse):
def __init__(self, response: HTTPResponse = None, message: str = None):
"""Initialize the InfluxDBError handler."""
if response is not None:
self.response = response
self.message = self._get_message(response)
self.retry_after = response.getheader('Retry-After')
else:
self.response = None
self.message = 'no response'
self.message = message or 'no response'
self.retry_after = None
super().__init__(self.message)

Expand Down
8 changes: 7 additions & 1 deletion influxdb_client/client/util/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ def get_org_query_param(org, client, required_id=False):
_org = _org.id
if required_id and _org and not _is_id(_org):
try:
return client.organizations_api().find_organizations(org=_org)[0].id
organizations = client.organizations_api().find_organizations(org=_org)
if len(organizations) < 1:
from influxdb_client.client.exceptions import InfluxDBError
message = f"The client cannot find organization with name: '{_org}' " \
"to determine their ID. Are you using token with sufficient permission?"
raise InfluxDBError(response=None, message=message)
return organizations[0].id
except ApiException:
return None

Expand Down
23 changes: 22 additions & 1 deletion tests/test_Helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from influxdb_client import InfluxDBClient, Organization
import pytest

from influxdb_client import InfluxDBClient, Organization, PermissionResource, Permission
# noinspection PyProtectedMember
from influxdb_client.client.exceptions import InfluxDBError
from influxdb_client.client.util.helpers import get_org_query_param, _is_id
from tests.base_test import BaseTest

Expand Down Expand Up @@ -36,3 +39,21 @@ def test_both_none(self):
self.client = InfluxDBClient(url=self.client.url, token="my-token")
org = get_org_query_param(None, self.client)
self.assertIsNone(org)

def test_not_permission_to_read_org(self):
# Create Token without permission to read Organizations
resource = PermissionResource(type="buckets", org_id=self.find_my_org().id)
authorization = self.client \
.authorizations_api() \
.create_authorization(org_id=self.find_my_org().id,
permissions=[Permission(resource=resource, action="read"),
Permission(resource=resource, action="write")])
self.client.close()

# Initialize client without permission to read Organizations
self.client = InfluxDBClient(url=self.client.url, token=authorization.token)

with pytest.raises(InfluxDBError) as e:
get_org_query_param("my-org", self.client, required_id=True)
assert "The client cannot find organization with name: 'my-org' to determine their ID. Are you using token " \
"with sufficient permission?" in f"{e.value} "