From a4c0811064a846d06b4bc5697539fd17eec5ca80 Mon Sep 17 00:00:00 2001 From: PetrusBellmonte Date: Tue, 31 Mar 2026 20:29:39 +0200 Subject: [PATCH 1/2] Add MessageReceiveError exception and update get_cached_messages use it --- python_ntfy/_exceptions.py | 12 ++++++++++-- python_ntfy/_get_functions.py | 21 ++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/python_ntfy/_exceptions.py b/python_ntfy/_exceptions.py index cea82fa..cb7b57c 100644 --- a/python_ntfy/_exceptions.py +++ b/python_ntfy/_exceptions.py @@ -1,5 +1,5 @@ -class MessageSendError(Exception): - """Exception raised when a message fails to send.""" +class NtfyMessageError(Exception): + """Base exception for ntfy message errors.""" def __init__(self, message: str) -> None: """Initialize the exception. @@ -9,3 +9,11 @@ def __init__(self, message: str) -> None: """ self.message = message super().__init__(self.message) + + +class MessageSendError(NtfyMessageError): + """Exception raised when a message fails to send.""" + + +class MessageReceiveError(NtfyMessageError): + """Exception raised when a message fails to retrieved.""" diff --git a/python_ntfy/_get_functions.py b/python_ntfy/_get_functions.py index ab255b8..5f2bac5 100644 --- a/python_ntfy/_get_functions.py +++ b/python_ntfy/_get_functions.py @@ -2,6 +2,8 @@ import requests +from python_ntfy._exceptions import MessageReceiveError + def get_cached_messages( self, @@ -19,6 +21,9 @@ def get_cached_messages( Returns: A list of messages. + Raises: + MessageReceiveError: If the request fails or the response is invalid. + Examples: >>> response = client.get(since="all") @@ -34,16 +39,18 @@ def get_cached_messages( if since: params.update({"since": since}) - response = [ - json.loads(line) - for line in requests.get( + try: + response = requests.get( url=self.url + "/json", params=params, auth=self._auth, timeout=timeout_seconds, ) - .text.strip() - .splitlines() - ] + response.raise_for_status() + messages = [json.loads(line) for line in response.text.strip().splitlines()] + except requests.exceptions.RequestException as e: + error_message = f"Failed to receive messages: {e}" + raise MessageReceiveError(error_message) from e + # Reverse the list so that the most recent notification is first - return sorted(response, key=lambda x: x["time"], reverse=True) + return sorted(messages, key=lambda x: x["time"], reverse=True) From f1ae016f354f57bd2af7b346e76e6f7e3f41c6c6 Mon Sep 17 00:00:00 2001 From: PetrusBellmonte Date: Tue, 31 Mar 2026 20:30:32 +0200 Subject: [PATCH 2/2] Add tests for failed sending and receiving --- tests/test_get_messages.py | 9 +++++++++ tests/test_send_file.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/tests/test_get_messages.py b/tests/test_get_messages.py index 4615710..37e629e 100644 --- a/tests/test_get_messages.py +++ b/tests/test_get_messages.py @@ -15,6 +15,7 @@ import pytest from python_ntfy import NtfyClient +from python_ntfy._exceptions import MessageReceiveError from .helpers import random_string @@ -60,3 +61,11 @@ async def test_get_topic_with_scheduled(localhost_server_no_auth, no_auth) -> No assert response[0]["topic"] == topic assert response[0]["message"] == message assert response[0]["time"] == int(ts.timestamp()) + + +@pytest.mark.asyncio +async def test_authentication_error(localhost_server_auth, no_auth) -> None: + topic = random_string(5) + ntfy = NtfyClient(topic=topic) + with pytest.raises(MessageReceiveError): + ntfy.get_cached_messages() diff --git a/tests/test_send_file.py b/tests/test_send_file.py index f08fec8..66e0043 100644 --- a/tests/test_send_file.py +++ b/tests/test_send_file.py @@ -52,3 +52,9 @@ def test_send_file_not_found() -> None: ntfy = NtfyClient(topic=topic) with raises(MessageSendError): ntfy.send_file(file="not_found.txt") + + +def test_send_file_auth_error(localhost_server_auth, no_auth) -> None: + ntfy = NtfyClient(topic=topic) + with raises(MessageSendError): + ntfy.send_file("tests/assets/test_text.txt", email="test@test.com")