Skip to content

Commit

Permalink
Merge pull request #53 from alan-turing-institute/update-debug-messages
Browse files Browse the repository at this point in the history
Better debug messages
  • Loading branch information
jemrobinson authored Oct 25, 2024
2 parents 6dcd420 + 17da39d commit 6200177
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion apricot/models/ldap_attribute_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

if TYPE_CHECKING:
from apricot.models import LDAPObjectClass
from apricot.types import JSONDict, LDAPAttributeDict
from apricot.typedefs import JSONDict, LDAPAttributeDict


class LDAPAttributeAdaptor:
Expand Down
2 changes: 1 addition & 1 deletion apricot/oauth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from apricot.types import LDAPAttributeDict, LDAPControlTuple
from apricot.typedefs import LDAPAttributeDict, LDAPControlTuple

from .enums import OAuthBackend
from .keycloak_client import KeycloakClient
Expand Down
14 changes: 9 additions & 5 deletions apricot/oauth/keycloak_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import operator
from typing import Any, Self, cast

from apricot.types import JSONDict
from twisted.python import log

from apricot.typedefs import JSONDict

from .oauth_client import OAuthClient

Expand Down Expand Up @@ -99,8 +101,9 @@ def groups(self: Self) -> list[JSONDict]:
user["username"] for user in cast(list[JSONDict], members)
]
output.append(attributes)
except KeyError:
pass
except KeyError as exc:
msg = f"Failed to process group {group_dict} due to a missing key {exc}."
log.msg(msg)
return output

def users(self: Self) -> list[JSONDict]:
Expand Down Expand Up @@ -170,6 +173,7 @@ def users(self: Self) -> list[JSONDict]:
attributes["uid"] = username
attributes["uidNumber"] = user_dict["attributes"]["uid"][0]
output.append(attributes)
except KeyError:
pass
except KeyError as exc:
msg = f"Failed to process user {user_dict} due to a missing key {exc}."
log.msg(msg)
return output
7 changes: 4 additions & 3 deletions apricot/oauth/microsoft_entra_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from twisted.python import log

from apricot.types import JSONDict
from apricot.typedefs import JSONDict

from .oauth_client import OAuthClient

Expand Down Expand Up @@ -114,6 +114,7 @@ def users(self: Self) -> list[JSONDict]:
attributes["uid"] = uid or None
attributes["uidNumber"] = user_uid
output.append(attributes)
except KeyError:
pass
except KeyError as exc:
msg = f"Failed to process user {user_dict} due to a missing key {exc}."
log.msg(msg)
return output
8 changes: 4 additions & 4 deletions apricot/oauth/oauth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

if TYPE_CHECKING:
from apricot.cache import UidCache
from apricot.types import JSONDict
from apricot.typedefs import JSONDict


class OAuthClient(ABC):
Expand Down Expand Up @@ -162,8 +162,8 @@ def query_(*args: Any, **kwargs: Any) -> requests.Response:
try:
result = query_(*args, **kwargs)
result.raise_for_status()
except (TokenExpiredError, requests.exceptions.HTTPError):
log.msg("Authentication token has expired.")
except (TokenExpiredError, requests.exceptions.HTTPError) as exc:
log.msg(f"Authentication token is invalid.\n{exc!s}")
self.bearer_token_ = None
result = query_(*args, **kwargs)
if result.status_code == HTTPStatus.NO_CONTENT:
Expand All @@ -181,7 +181,7 @@ def verify(self: Self, username: str, password: str) -> bool:
client_secret=self.client_secret,
)
except InvalidGrantError as exc:
log.msg(f"Authentication failed for user '{username}'.\n{exc}")
log.msg(f"Authentication failed for user '{username}'.\n{exc!s}")
return False
else:
return True
2 changes: 1 addition & 1 deletion apricot/oauth/oauth_data_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

if TYPE_CHECKING:

from apricot.types import JSONDict
from apricot.typedefs import JSONDict

from .oauth_client import OAuthClient

Expand Down
1 change: 0 additions & 1 deletion apricot/types.py → apricot/typedefs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any

JSONDict = dict[str, Any]
JSONKey = list[Any] | dict[str, Any] | Any
LDAPAttributeDict = dict[str, list[str]]
LDAPControlTuple = tuple[str, bool, Any]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ detached = true
dependencies = [
"black~=24.2",
"mypy~=1.8",
"ruff~=0.3",
"ruff~=0.7",
"types-oauthlib~=3.2",
"types-redis~=4.6",
"types-requests~=2.31",
Expand Down

0 comments on commit 6200177

Please sign in to comment.