From 41c5355d1bf53b1ffde4d6b1ad6bae202e861e94 Mon Sep 17 00:00:00 2001 From: dgaloop Date: Mon, 3 Jun 2024 15:14:31 +0300 Subject: [PATCH 1/2] Ensure message is propagated --- deeplake/client/client.py | 19 +++++-------------- deeplake/util/exceptions.py | 25 ++++++------------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/deeplake/client/client.py b/deeplake/client/client.py index b9b696ece7..9e511a3a2d 100644 --- a/deeplake/client/client.py +++ b/deeplake/client/client.py @@ -1,19 +1,13 @@ -import os - import deeplake import requests # type: ignore -import textwrap -from typing import Any, Optional, Dict, List, Union +from typing import Any, Optional, Dict from deeplake.util.exceptions import ( AgreementNotAcceptedError, AuthorizationException, - LoginException, - InvalidPasswordException, ManagedCredentialsNotFoundError, NotLoggedInAgreementError, ResourceNotFoundException, InvalidTokenException, - UserNotLoggedInException, TokenPermissionError, ) from deeplake.client.utils import ( @@ -125,11 +119,6 @@ def request( headers["hub-cli-version"] = self.version headers = {**headers, **self.auth_context.get_auth_headers()} - # clearer error than `ServerUnderMaintenence` - if json is not None and "password" in json and json["password"] is None: - # do NOT pass in the password here. `None` is explicitly typed. - raise InvalidPasswordException("Password cannot be `None`.") - status_code = None tries = 0 while status_code is None or (status_code in retry_status_codes and tries < 3): @@ -215,11 +204,13 @@ def get_dataset_credentials( raise NotLoggedInAgreementError from e else: try: - jwt.decode(self.token, options={"verify_signature": False}) + jwt.decode( + self.get_token(), options={"verify_signature": False} + ) except Exception: raise InvalidTokenException - raise TokenPermissionError() + raise TokenPermissionError(e.original_message) raise full_url = response.get("path") repository = response.get("repository") diff --git a/deeplake/util/exceptions.py b/deeplake/util/exceptions.py index 4398e41dde..6fd72a1b10 100644 --- a/deeplake/util/exceptions.py +++ b/deeplake/util/exceptions.py @@ -1,7 +1,9 @@ +import requests import numpy as np -import deeplake from typing import Any, List, Sequence, Tuple, Optional, Union +import deeplake + class ExternalCommandError(Exception): def __init__(self, command: str, status: int): @@ -189,14 +191,6 @@ def __init__(self, message): super().__init__(message) -class LoginException(Exception): - def __init__( - self, - message="Error while logging in, invalid auth token. Please try logging in again.", - ): - super().__init__(message) - - class UserNotLoggedInException(Exception): def __init__(self): message = ( @@ -237,21 +231,14 @@ def __init__(self, message="Authentication failed. Please try logging in again." class AuthorizationException(Exception): def __init__( self, - message="You are not authorized to access this resource on Activeloop Server.", - response=None, + message: Optional[str] = None, + response: Optional[requests.Response] = None, ): + self.original_message = message self.response = response super().__init__(message) -class InvalidPasswordException(AuthorizationException): - def __init__( - self, - message="The password you provided was invalid.", - ): - super().__init__(message) - - class CouldNotCreateNewDatasetException(AuthorizationException): def __init__( self, From fbf408787335c2e05d97b300b50a6cc3eb21c760 Mon Sep 17 00:00:00 2001 From: dgaloop Date: Mon, 3 Jun 2024 16:02:12 +0300 Subject: [PATCH 2/2] Add typeguard based on mypy suggestion --- deeplake/client/client.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/deeplake/client/client.py b/deeplake/client/client.py index 9e511a3a2d..628dacdb54 100644 --- a/deeplake/client/client.py +++ b/deeplake/client/client.py @@ -194,8 +194,11 @@ def get_dataset_credentials( ).json() except Exception as e: if isinstance(e, AuthorizationException): - response_data = e.response.json() - code = response_data.get("code") + code = -1 + if e.response is not None: + response_data = e.response.json() + code = response_data.get("code") + if code == 1: agreements = response_data["agreements"] agreements = [agreement["text"] for agreement in agreements]