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

Fix exception handling in ApiMethodWrapper.handle_neptune_http_errors #1469

Merged
merged 6 commits into from
Sep 13, 2023
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 @@ -6,6 +6,9 @@
### Changes
- Add handling of project limits [#1456](https://github.com/neptune-ai/neptune-client/pull/1456)

### Fixes
- Fix exception handling in `ApiMethodWrapper.handle_neptune_http_errors` ([#1469](https://github.com/neptune-ai/neptune-client/pull/1469))


## neptune 1.6.3

Expand Down
39 changes: 0 additions & 39 deletions src/neptune/api/exceptions_utils.py

This file was deleted.

14 changes: 2 additions & 12 deletions src/neptune/common/backends/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
from requests.exceptions import ChunkedEncodingError
from urllib3.exceptions import NewConnectionError

from neptune.api.exceptions_utils import handle_json_errors
from neptune.api.requests_utils import ensure_json_response
from neptune.common.envs import NEPTUNE_RETRIES_TIMEOUT_ENV
from neptune.common.exceptions import (
ClientHttpError,
Expand All @@ -48,7 +46,6 @@
NeptuneInvalidApiTokenException,
NeptuneSSLVerificationError,
Unauthorized,
WritingToArchivedProjectException,
)
from neptune.common.utils import reset_internal_ssl_state

Expand Down Expand Up @@ -110,15 +107,8 @@ def wrapper(*args, **kwargs):
continue
except HTTPUnauthorized:
raise Unauthorized()
except HTTPForbidden as e:
handle_json_errors(
content=ensure_json_response(e.response),
error_processors={
"WRITE_ACCESS_DENIED_TO_ARCHIVED_PROJECT": lambda _: WritingToArchivedProjectException()
},
source_exception=e,
default_exception=Forbidden(),
)
except HTTPForbidden:
raise Forbidden()
except HTTPClientError as e:
raise ClientHttpError(e.status_code, e.response.text) from e
except requests.exceptions.RequestException as e:
Expand Down
1 change: 1 addition & 0 deletions src/neptune/internal/backends/hosted_file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def upload_raw_data(
):
raise NeptuneLimitExceedException(reason=response.json().get("title", "Unknown reason"))
response.raise_for_status()

return response.content


Expand Down
29 changes: 20 additions & 9 deletions src/neptune/internal/backends/swagger_client_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
__all__ = ["ApiMethodWrapper", "SwaggerClientWrapper"]

from collections.abc import Callable
from typing import Optional
from typing import (
Dict,
Optional,
)

from bravado.client import SwaggerClient
from bravado.exception import HTTPError

from neptune.api.exceptions_utils import handle_json_errors
from neptune.api.requests_utils import ensure_json_response
from neptune.common.exceptions import NeptuneAuthTokenExpired
from neptune.common.exceptions import (
NeptuneAuthTokenExpired,
WritingToArchivedProjectException,
)
from neptune.exceptions import (
NeptuneFieldCountLimitExceedException,
NeptuneLimitExceedException,
Expand All @@ -48,7 +53,7 @@ def handle_neptune_http_errors(response, exception: Optional[HTTPError] = None):
ProjectsLimitReached,
)

error_processors: dict[str, Callable[[dict], Exception]] = {
error_processors: Dict[str, Callable[[Dict], Exception]] = {
"ATTRIBUTES_PER_EXPERIMENT_LIMIT_EXCEEDED": lambda response_body: NeptuneFieldCountLimitExceedException(
limit=response_body.get("limit", "<unknown limit>"),
container_type=response_body.get("experimentType", "object"),
Expand Down Expand Up @@ -86,13 +91,19 @@ def handle_neptune_http_errors(response, exception: Optional[HTTPError] = None):
"LIMIT_OF_ACTIVE_PROJECTS_REACHED": lambda response_body: ActiveProjectsLimitReachedException(
currentQuota=response_body.get("currentQuota", "<unknown quota>")
),
"WRITE_ACCESS_DENIED_TO_ARCHIVED_PROJECT": lambda _: WritingToArchivedProjectException(),
}

handle_json_errors(
content=ensure_json_response(response),
error_processors=error_processors,
source_exception=exception,
)
body = ensure_json_response(response)
error_type: Optional[str] = body.get("errorType")
error_processor = error_processors.get(error_type)
if error_processor:
if exception:
raise error_processor(body) from exception
raise error_processor(body)

if exception:
raise exception

def __call__(self, *args, **kwargs):
try:
Expand Down
86 changes: 0 additions & 86 deletions tests/unit/neptune/new/api/test_exceptions_utils.py

This file was deleted.

Loading