Skip to content
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

ADCM-6240: Redesign the error detection mechanism and make them more user-friendly #57

Merged
merged 3 commits into from
Dec 26, 2024
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
24 changes: 12 additions & 12 deletions adcm_aio_client/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class ADCMClientError(Exception):
pass


class WaitTimeoutError(ADCMClientError):
pass


# Session


Expand All @@ -25,10 +29,6 @@ class ClientInitError(ADCMClientError):
# Version


class VersionRetrievalError(ADCMClientError):
pass


class NotSupportedVersionError(ADCMClientError):
pass

Expand All @@ -44,7 +44,7 @@ class NoCredentialsError(RequesterError):
pass


class WrongCredentialsError(RequesterError):
class AuthenticationError(RequesterError):
pass


Expand All @@ -64,31 +64,31 @@ class ResponseDataConversionError(RequesterError):
pass


class ResponseError(RequesterError):
class UnknownError(RequesterError):
pass


class BadRequestError(ResponseError):
class BadRequestError(UnknownError):
pass


class UnauthorizedError(ResponseError):
class UnauthorizedError(UnknownError):
pass


class ForbiddenError(ResponseError):
class PermissionDeniedError(UnknownError):
pass


class NotFoundError(ResponseError):
class NotFoundError(UnknownError):
pass


class ConflictError(ResponseError):
class ConflictError(UnknownError):
pass


class ServerError(ResponseError):
class ServerError(UnknownError):
pass


Expand Down
4 changes: 2 additions & 2 deletions adcm_aio_client/core/objects/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from asyncstdlib.functools import cached_property as async_cached_property # noqa: N813

from adcm_aio_client.core.actions._objects import Action
from adcm_aio_client.core.errors import InvalidFilterError, NotFoundError
from adcm_aio_client.core.errors import InvalidFilterError, NotFoundError, WaitTimeoutError
from adcm_aio_client.core.filters import (
ALL_OPERATIONS,
COMMON_OPERATIONS,
Expand Down Expand Up @@ -532,7 +532,7 @@ async def wait(
if timeout:
message = f"{message} in {timeout} seconds with {poll_interval} second interval"

raise TimeoutError(message)
raise WaitTimeoutError(message)

async def terminate(self: Self) -> None:
await self._requester.post(*self.get_own_path(), "terminate", data={})
Expand Down
16 changes: 8 additions & 8 deletions adcm_aio_client/core/requesters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
import httpx

from adcm_aio_client.core.errors import (
AuthenticationError,
BadRequestError,
ConflictError,
ForbiddenError,
LoginError,
LogoutError,
NoCredentialsError,
NotFoundError,
OperationError,
PermissionDeniedError,
ResponseDataConversionError,
ResponseError,
RetryRequestError,
ServerError,
UnauthorizedError,
WrongCredentialsError,
UnknownError,
)
from adcm_aio_client.core.types import (
Credentials,
Expand Down Expand Up @@ -92,7 +92,7 @@ def _get_json_data(self: Self) -> Json:
STATUS_ERRORS_MAP = {
400: BadRequestError,
401: UnauthorizedError,
403: ForbiddenError,
403: PermissionDeniedError,
404: NotFoundError,
409: ConflictError,
500: ServerError,
Expand All @@ -104,7 +104,7 @@ def convert_exceptions(func: DoRequestFunc) -> DoRequestFunc:
async def wrapper(*arg: Params.args, **kwargs: Params.kwargs) -> httpx.Response:
response = await func(*arg, **kwargs)
if response.status_code >= 300:
error_cls = STATUS_ERRORS_MAP.get(response.status_code, ResponseError)
error_cls = STATUS_ERRORS_MAP.get(response.status_code, UnknownError)
# not safe, because can be not json
try:
message = response.json()
Expand Down Expand Up @@ -173,8 +173,8 @@ async def login(self: Self, credentials: Credentials) -> Self:
f"Login to ADCM at {self.client.base_url} has failed for "
f"user {credentials.username} most likely due to incorrect credentials"
)
raise WrongCredentialsError(message) from e
except ResponseError as e:
raise AuthenticationError(message) from e
except UnknownError as e:
message = f"Login to ADCM at {self.client.base_url} has failed for user {credentials.username}: {e}"
raise LoginError(message) from e

Expand All @@ -190,7 +190,7 @@ async def logout(self: Self) -> Self:
try:
request_coro = self.client.post(url=logout_url, data={})
await self._do_request(request_coro)
except ResponseError as e:
except UnknownError as e:
message = f"Logout from ADCM at {self.client.base_url} has failed"
raise LogoutError(message) from e

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ConfigNoParameterError,
NoConfigInActionError,
NoMappingInActionError,
ResponseError,
UnknownError,
)
from adcm_aio_client.core.filters import Filter
from adcm_aio_client.core.mapping.refresh import apply_remote_changes
Expand Down Expand Up @@ -211,7 +211,7 @@ async def _test_upgrade_with_mapping(context: Context) -> None:
config["params", ParameterGroup]["pass", Parameter].set("notenough")

# quite strange pick of response in here in ADCM, so generalized expected error
with pytest.raises(ResponseError, match=".*config key.*is required"):
with pytest.raises(UnknownError, match=".*config key.*is required"):
await upgrade.run()

config["string_field", Parameter].set("useless yet required")
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_requesters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
import pytest_asyncio

from adcm_aio_client.core.errors import ResponseDataConversionError, ResponseError
from adcm_aio_client.core.errors import ResponseDataConversionError, UnknownError
from adcm_aio_client.core.requesters import DefaultRequester, HTTPXRequesterResponse
from adcm_aio_client.core.types import RetryPolicy

Expand Down Expand Up @@ -93,7 +93,7 @@ async def test_raising_client_error_for_status(
partial(requester.patch, data={}),
requester.delete,
):
with pytest.raises(ResponseError):
with pytest.raises(UnknownError):
await method()


Expand Down