Skip to content

Commit 1471e90

Browse files
authored
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 <[email protected]>
1 parent 3cde102 commit 1471e90

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.17.1 - 2024-09-06]
6+
7+
### Fixed
8+
9+
- NextcloudApp: `nc.log` now suppresses all exceptions to safe call it anywhere in your app.
10+
511
## [0.17.0 - 2024-09-05]
612

713
### Added

nc_py_api/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Version of nc_py_api."""
22

3-
__version__ = "0.17.0"
3+
__version__ = "0.17.1"

nc_py_api/nextcloud.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,13 @@ def log(self, log_lvl: LogLvl, content: str) -> None:
352352
"""Writes log to the Nextcloud log file."""
353353
if self.check_capabilities("app_api"):
354354
return
355-
if int(log_lvl) < self.capabilities["app_api"].get("loglevel", 0):
355+
int_log_lvl = int(log_lvl)
356+
if int_log_lvl < 0 or int_log_lvl > 4:
357+
raise ValueError("Invalid `log_lvl` value")
358+
if int_log_lvl < self.capabilities["app_api"].get("loglevel", 0):
356359
return
357-
self._session.ocs("POST", f"{self._session.ae_url}/log", json={"level": int(log_lvl), "message": content})
360+
with contextlib.suppress(Exception):
361+
self._session.ocs("POST", f"{self._session.ae_url}/log", json={"level": int_log_lvl, "message": content})
358362

359363
def users_list(self) -> list[str]:
360364
"""Returns list of users on the Nextcloud instance."""
@@ -482,9 +486,15 @@ async def log(self, log_lvl: LogLvl, content: str) -> None:
482486
"""Writes log to the Nextcloud log file."""
483487
if await self.check_capabilities("app_api"):
484488
return
485-
if int(log_lvl) < (await self.capabilities)["app_api"].get("loglevel", 0):
489+
int_log_lvl = int(log_lvl)
490+
if int_log_lvl < 0 or int_log_lvl > 4:
491+
raise ValueError("Invalid `log_lvl` value")
492+
if int_log_lvl < (await self.capabilities)["app_api"].get("loglevel", 0):
486493
return
487-
await self._session.ocs("POST", f"{self._session.ae_url}/log", json={"level": int(log_lvl), "message": content})
494+
with contextlib.suppress(Exception):
495+
await self._session.ocs(
496+
"POST", f"{self._session.ae_url}/log", json={"level": int_log_lvl, "message": content}
497+
)
488498

489499
async def users_list(self) -> list[str]:
490500
"""Returns list of users on the Nextcloud instance."""

tests/actual_tests/logs_test.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import pytest
55

6-
from nc_py_api import NextcloudException
76
from nc_py_api.ex_app import LogLvl
87

98

@@ -34,13 +33,13 @@ async def test_loglvl_str_async(anc_app):
3433

3534

3635
def test_invalid_log_level(nc_app):
37-
with pytest.raises(NextcloudException):
36+
with pytest.raises(ValueError):
3837
nc_app.log(5, "wrong log level") # noqa
3938

4039

4140
@pytest.mark.asyncio(scope="session")
4241
async def test_invalid_log_level_async(anc_app):
43-
with pytest.raises(NextcloudException):
42+
with pytest.raises(ValueError):
4443
await anc_app.log(5, "wrong log level") # noqa
4544

4645

0 commit comments

Comments
 (0)