From 1471e90b7d9bde4acef2e9a856f2e908372cafc3 Mon Sep 17 00:00:00 2001 From: Alexander Piskun <13381981+bigcat88@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:05:33 +0300 Subject: [PATCH] Suppress exceptions in `nc.log` (#293) Changes proposed in this pull request: * Suppress all exceptions during `nc.log` It's strange that I didn't do this before, without it we won't be able to use logging during exception handling. --------- Signed-off-by: Alexander Piskun --- CHANGELOG.md | 6 ++++++ nc_py_api/_version.py | 2 +- nc_py_api/nextcloud.py | 18 ++++++++++++++---- tests/actual_tests/logs_test.py | 5 ++--- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b748e472..6e5a0565 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. +## [0.17.1 - 2024-09-06] + +### Fixed + +- NextcloudApp: `nc.log` now suppresses all exceptions to safe call it anywhere in your app. + ## [0.17.0 - 2024-09-05] ### Added diff --git a/nc_py_api/_version.py b/nc_py_api/_version.py index dc3370e1..749f78f5 100644 --- a/nc_py_api/_version.py +++ b/nc_py_api/_version.py @@ -1,3 +1,3 @@ """Version of nc_py_api.""" -__version__ = "0.17.0" +__version__ = "0.17.1" diff --git a/nc_py_api/nextcloud.py b/nc_py_api/nextcloud.py index a079554f..a4f9baa7 100644 --- a/nc_py_api/nextcloud.py +++ b/nc_py_api/nextcloud.py @@ -352,9 +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 - self._session.ocs("POST", f"{self._session.ae_url}/log", json={"level": int(log_lvl), "message": content}) + with contextlib.suppress(Exception): + 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.""" @@ -482,9 +486,15 @@ 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 - await self._session.ocs("POST", f"{self._session.ae_url}/log", json={"level": int(log_lvl), "message": content}) + with contextlib.suppress(Exception): + await self._session.ocs( + "POST", f"{self._session.ae_url}/log", json={"level": int_log_lvl, "message": content} + ) async def users_list(self) -> list[str]: """Returns list of users on the Nextcloud instance.""" diff --git a/tests/actual_tests/logs_test.py b/tests/actual_tests/logs_test.py index 4dfc6b88..74d35a2d 100644 --- a/tests/actual_tests/logs_test.py +++ b/tests/actual_tests/logs_test.py @@ -3,7 +3,6 @@ import pytest -from nc_py_api import NextcloudException from nc_py_api.ex_app import LogLvl @@ -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