Skip to content

Commit

Permalink
Properly handle expired oauth token
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Niezurawski committed Feb 27, 2023
1 parent 6c2ec5a commit b4ba3bf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## neptune 1.0.2 [Unreleased]

### Fixes
- Properly handle expired oauth token ([#1271](https://github.com/neptune-ai/neptune-client/pull/1271))

## neptune 1.0.1

### Fixes
Expand Down
4 changes: 3 additions & 1 deletion src/neptune/common/backends/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
NeptuneConnectionLostException,
NeptuneInvalidApiTokenException,
NeptuneSSLVerificationError,
Unauthorized,
Unauthorized, NeptuneAuthTokenExpired,
)

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -84,6 +84,8 @@ def wrapper(*args, **kwargs):
time.sleep(min(2 ** min(10, retry), MAX_RETRY_TIME))
last_exception = e
continue
except NeptuneAuthTokenExpired:
continue
except HTTPUnauthorized:
raise Unauthorized()
except HTTPForbidden:
Expand Down
5 changes: 5 additions & 0 deletions src/neptune/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ def __init__(self):
super().__init__(message.format(**STYLES))


class NeptuneAuthTokenExpired(Unauthorized):
def __init__(self):
super().__init__("Authorization token expired")


class InternalServerError(NeptuneApiException):
def __init__(self, response):
message = """
Expand Down
4 changes: 4 additions & 0 deletions src/neptune/internal/backends/swagger_client_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from bravado.client import SwaggerClient
from bravado.exception import HTTPError

from neptune.common.exceptions import NeptuneAuthTokenExpired
from neptune.exceptions import (
NeptuneFieldCountLimitExceedException,
NeptuneLimitExceedException,
Expand All @@ -35,6 +36,7 @@ class ApiMethodWrapper:
PROJECT_KEY_INVALID = "PROJECT_KEY_INVALID"
PROJECT_NAME_INVALID = "PROJECT_NAME_INVALID"
EXPERIMENT_NOT_FOUND = "EXPERIMENT_NOT_FOUND"
AUTHORIZATION_TOKEN_EXPIRED = "AUTHORIZATION_TOKEN_EXPIRED"

def __init__(self, api_method):
self._api_method = api_method
Expand All @@ -53,6 +55,8 @@ def handle_neptune_http_errors(response, exception: Optional[HTTPError] = None):
container_type=body.get("experimentType", "object"),
identifier=body.get("experimentQualifiedName", "<unknown identifier>"),
)
elif error_type == ApiMethodWrapper.AUTHORIZATION_TOKEN_EXPIRED:
raise NeptuneAuthTokenExpired() from exception
elif error_type == ApiMethodWrapper.WORKSPACE_IN_READ_ONLY_MODE:
raise NeptuneLimitExceedException(reason=body.get("title", "Unknown reason")) from exception
elif error_type == ApiMethodWrapper.INCORRECT_IDENTIFIER:
Expand Down

0 comments on commit b4ba3bf

Please sign in to comment.