Skip to content
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
12 changes: 10 additions & 2 deletions python_ntfy/_exceptions.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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."""
21 changes: 14 additions & 7 deletions python_ntfy/_get_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import requests

from python_ntfy._exceptions import MessageReceiveError


def get_cached_messages(
self,
Expand All @@ -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")

Expand 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)
9 changes: 9 additions & 0 deletions tests/test_get_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pytest

from python_ntfy import NtfyClient
from python_ntfy._exceptions import MessageReceiveError

from .helpers import random_string

Expand Down Expand Up @@ -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()
6 changes: 6 additions & 0 deletions tests/test_send_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Loading