Skip to content

Commit

Permalink
Raise more informative error when fetching devices with invalid org U…
Browse files Browse the repository at this point in the history
…ID (#365)

* Raise more informative error when fetching devices with invalid org UID

* Update changelog
  • Loading branch information
peterbriggs42 committed Sep 14, 2021
1 parent 0d0a472 commit e75b34c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
### Fixed

- Issue where `sdk.users.get_roles()` was using a deprecated API.
- Issue where `sdk.devices.get_page()` raises a vague error message when provided with invalid org UID.

## 1.14.1 - 2021-04-29

Expand Down
9 changes: 7 additions & 2 deletions src/py42/services/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from py42 import settings
from py42.clients.settings.device_settings import DeviceSettings
from py42.exceptions import Py42BadRequestError
from py42.exceptions import Py42OrgNotFoundError
from py42.services import BaseService
from py42.services import handle_active_legal_hold_error
from py42.services.util import get_all_pages
Expand Down Expand Up @@ -69,8 +70,12 @@ def get_page(
"pgSize": page_size,
"q": q,
}

return self._connection.get(uri, params=params)
try:
return self._connection.get(uri, params=params)
except Py42BadRequestError as err:
if "Unable to find org" in str(err.response.text):
raise Py42OrgNotFoundError(err, org_uid)
raise

def get_all(
self,
Expand Down
16 changes: 16 additions & 0 deletions tests/services/test_devices.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import pytest
from requests import HTTPError
from requests import Response
from tests.conftest import create_mock_error
from tests.conftest import create_mock_response

import py42
from py42.exceptions import Py42ActiveLegalHoldError
from py42.exceptions import Py42BadRequestError
from py42.exceptions import Py42OrgNotFoundError
from py42.services.devices import DeviceService

COMPUTER_URI = "/api/Computer"
Expand Down Expand Up @@ -144,3 +146,17 @@ def side_effect(url, json):

expected = "Cannot deactivate the device with ID 1234 as the device is involved in a legal hold matter."
assert str(err.value) == expected

def test_get_page_when_org_not_found_raises_expected_error(
self, mocker, mock_connection
):
text = '[{"name":"SYSTEM","description":"Unable to find org"}]'
mock_connection.get.side_effect = create_mock_error(
Py42BadRequestError, mocker, text
)
service = DeviceService(mock_connection)

with pytest.raises(Py42OrgNotFoundError) as err:
service.get_page(1, org_uid="TestOrgUid")

assert str(err.value) == "The organization with UID 'TestOrgUid' was not found."

0 comments on commit e75b34c

Please sign in to comment.