-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Makes error handler method (raise_ais_error or raise_authn_error with raise_ais_error as default) a parameter of RequestClient and modified both Client and AuthNClient to initialize RequestClient with proper error handler. - Separates errors and handling by package - Minimally changes client-side usage (usage of AuthNClient and Client remains the same, only RequestClient usage changes but rarely used by user, only internally by AuthNClient and Client). Signed-off-by: Ryan Koo <[email protected]>
- Loading branch information
Showing
13 changed files
with
228 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# | ||
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
|
||
|
||
class AuthNError(Exception): | ||
""" | ||
Raised when an error occurs during a query to the AuthN cluster. | ||
""" | ||
|
||
def __init__(self, status_code: int, message: str): | ||
super().__init__(f"STATUS:{status_code}, MESSAGE:{message}") | ||
self.status_code = status_code | ||
self.message = message | ||
|
||
|
||
# pylint: disable=unused-variable | ||
class ErrRoleNotFound(AuthNError): | ||
""" | ||
Raised when a role is expected but not found. | ||
""" | ||
|
||
|
||
# pylint: disable=unused-variable | ||
class ErrRoleAlreadyExists(AuthNError): | ||
""" | ||
Raised when a role is created but already exists. | ||
""" | ||
|
||
|
||
# pylint: disable=unused-variable | ||
class ErrUserNotFound(AuthNError): | ||
""" | ||
Raised when a user is expected but not found. | ||
""" | ||
|
||
|
||
# pylint: disable=unused-variable | ||
class ErrUserAlreadyExists(AuthNError): | ||
""" | ||
Raised when a user is created but already exists. | ||
""" | ||
|
||
|
||
# pylint: disable=unused-variable | ||
class ErrClusterNotFound(AuthNError): | ||
""" | ||
Raised when a cluster is expected but not found. | ||
""" | ||
|
||
|
||
# pylint: disable=unused-variable | ||
class ErrClusterAlreadyRegistered(AuthNError): | ||
""" | ||
Raised when a cluster is already registered. | ||
""" | ||
|
||
|
||
# pylint: disable=unused-variable | ||
class ErrUserInvalidCredentials(AuthNError): | ||
""" | ||
Raised when invalid credentials for a user are provided. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
|
||
import pydantic.tools | ||
|
||
from aistore.sdk.utils import HttpError | ||
from aistore.sdk.authn.errors import ( | ||
AuthNError, | ||
ErrClusterNotFound, | ||
ErrClusterAlreadyRegistered, | ||
ErrRoleNotFound, | ||
ErrRoleAlreadyExists, | ||
ErrUserNotFound, | ||
ErrUserAlreadyExists, | ||
ErrUserInvalidCredentials, | ||
) | ||
|
||
|
||
def raise_authn_error(text: str): | ||
""" | ||
Raises an AuthN-specific error based on the API response text. | ||
Args: | ||
text (str): The raw text of the API response containing error details. | ||
Raises: | ||
AuthNError: If the error doesn't match any specific conditions. | ||
ErrClusterNotFound: If the error message indicates a missing cluster. | ||
ErrRoleNotFound: If the error message indicates a missing role. | ||
ErrUserNotFound: If the error message indicates a missing user. | ||
ErrRoleAlreadyExists: If the error message indicates a role already exists. | ||
ErrUserAlreadyExists: If the error message indicates a user already exists. | ||
ErrClusterAlreadyRegistered: If the error message indicates the cluster is already registered. | ||
ErrUserInvalidCredentials: If the error message indicates invalid user credentials. | ||
""" | ||
err = pydantic.tools.parse_raw_as(HttpError, text) | ||
if 400 <= err.status <= 500: | ||
if "does not exist" in err.message: | ||
if "cluster" in err.message: | ||
raise ErrClusterNotFound(err.status, err.message) | ||
if "role" in err.message: | ||
raise ErrRoleNotFound(err.status, err.message) | ||
if "user" in err.message: | ||
raise ErrUserNotFound(err.status, err.message) | ||
elif "already exists" in err.message: | ||
if "role" in err.message: | ||
raise ErrRoleAlreadyExists(err.status, err.message) | ||
if "user" in err.message: | ||
raise ErrUserAlreadyExists(err.status, err.message) | ||
elif "already registered" in err.message: | ||
raise ErrClusterAlreadyRegistered(err.status, err.message) | ||
elif "invalid credentials" in err.message: | ||
raise ErrUserInvalidCredentials(err.status, err.message) | ||
raise AuthNError(err.status, err.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.