Skip to content

Suppress exceptions in nc.log #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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