Skip to content

Commit

Permalink
allow to use nc.log in exception handlers
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 committed Sep 5, 2024
1 parent 036d14c commit 241684c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 10 additions & 4 deletions nc_py_api/nextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,13 @@ def log(self, log_lvl: LogLvl, content: str) -> None:
"""Writes log to the Nextcloud log file."""
if self.check_capabilities("app_api"):
return
if int(log_lvl) < self.capabilities["app_api"].get("loglevel", 0):
int_log_lvl = int(log_lvl)
if int_log_lvl < 0 or int_log_lvl > 4:
raise ValueError("Invalid `log_lvl` value")
if int_log_lvl < self.capabilities["app_api"].get("loglevel", 0):
return
with contextlib.suppress(Exception):
self._session.ocs("POST", f"{self._session.ae_url}/log", json={"level": int(log_lvl), "message": content})
self._session.ocs("POST", f"{self._session.ae_url}/log", json={"level": int_log_lvl, "message": content})

def users_list(self) -> list[str]:
"""Returns list of users on the Nextcloud instance."""
Expand Down Expand Up @@ -483,11 +486,14 @@ async def log(self, log_lvl: LogLvl, content: str) -> None:
"""Writes log to the Nextcloud log file."""
if await self.check_capabilities("app_api"):
return
if int(log_lvl) < (await self.capabilities)["app_api"].get("loglevel", 0):
int_log_lvl = int(log_lvl)
if int_log_lvl < 0 or int_log_lvl > 4:
raise ValueError("Invalid `log_lvl` value")
if int_log_lvl < (await self.capabilities)["app_api"].get("loglevel", 0):
return
with contextlib.suppress(Exception):
await self._session.ocs(
"POST", f"{self._session.ae_url}/log", json={"level": int(log_lvl), "message": content}
"POST", f"{self._session.ae_url}/log", json={"level": int_log_lvl, "message": content}
)

async def users_list(self) -> list[str]:
Expand Down
5 changes: 2 additions & 3 deletions tests/actual_tests/logs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest

from nc_py_api import NextcloudException
from nc_py_api.ex_app import LogLvl


Expand Down Expand Up @@ -34,13 +33,13 @@ async def test_loglvl_str_async(anc_app):


def test_invalid_log_level(nc_app):
with pytest.raises(NextcloudException):
with pytest.raises(ValueError):
nc_app.log(5, "wrong log level") # noqa


@pytest.mark.asyncio(scope="session")
async def test_invalid_log_level_async(anc_app):
with pytest.raises(NextcloudException):
with pytest.raises(ValueError):
await anc_app.log(5, "wrong log level") # noqa


Expand Down

0 comments on commit 241684c

Please sign in to comment.