Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.
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
6 changes: 6 additions & 0 deletions fastapi_keycloak/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
UpdatePasswordException,
UpdateProfileException,
UpdateUserLocaleException,
UserNotFound,
VerifyEmailException,
)
from fastapi_keycloak.model import (
Expand Down Expand Up @@ -849,6 +850,11 @@ def get_user(self, user_id: str = None, query: str = "") -> KeycloakUser:
response = self._admin_request(
url=f"{self.users_uri}/{user_id}", method=HTTPMethod.GET
)
if response.status_code == status.HTTP_404_NOT_FOUND:
raise UserNotFound(
status_code = status.HTTP_404_NOT_FOUND,
reason=f"User with user_id[{user_id}] was not found"
)
return KeycloakUser(**response.json())

@result_or_error(response_model=KeycloakUser)
Expand Down
11 changes: 11 additions & 0 deletions fastapi_keycloak/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ def __init__(self, status_code: int, reason: str):
self.reason = reason
super().__init__(f"HTTP {status_code}: {reason}")

class UserNotFound(Exception):
"""Thrown when a user lookup fails.

Attributes:
status_code (int): The status code of the response received
reason (str): The reason why the requests did fail
"""
def __init__(self, status_code: int, reason: str):
self.status_code = status_code
self.reason = reason
super().__init__(f"HTTP {status_code}: {reason}")

class MandatoryActionException(HTTPException):
"""Throw if the exchange of username and password for an access token fails"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
UpdatePasswordException,
UpdateProfileException,
UpdateUserLocaleException,
UserNotFound,
VerifyEmailException,
)
from fastapi_keycloak.model import (
Expand Down Expand Up @@ -445,3 +446,10 @@ def test_login_exceptions(self, idp, action, exception, user):

# Clean up
idp.delete_user(user_id=user.id)

def test_user_not_found_exception(self, idp):

with pytest.raises(
UserNotFound
): # Expect the get to fail due to anon existant user
u = idp.get_user(user_id="some_non_existant_user_id")