Skip to content

Commit

Permalink
Suppress exceptions in nc.log (#293)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bigcat88 authored Sep 5, 2024
1 parent 3cde102 commit 1471e90
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nc_py_api/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version of nc_py_api."""

__version__ = "0.17.0"
__version__ = "0.17.1"
18 changes: 14 additions & 4 deletions nc_py_api/nextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
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 1471e90

Please sign in to comment.