Source code for ntfy_api.errors

 1import json
 2from typing import Union
 3
 4import httpx
 5
 6from .__version__ import *  # noqa: F401,F403
 7
 8__all__ = ("APIError",)
 9
10
[docs] 11class APIError(Exception): 12 """An error received from the ntfy API. 13 14 :param response: The :class:`httpx.Response` object that encountered 15 the error. 16 :type response: httpx.Response 17 :param stream: Whether or not the response is streaming. 18 :type stream: bool 19 20 """ 21 22 def __init__(self, response: httpx.Response, stream: bool) -> None: 23 try: 24 data: dict[str, Union[str, int]] = json.loads( 25 next(response.iter_lines()) if stream else response.content 26 ) 27 msg = "; ".join( 28 f"{k}={v!r}" 29 for k, v in ( 30 (k, data.get(k)) for k in ("http", "code", "error", "link") 31 ) 32 if v is not None 33 ) 34 super().__init__(f"Error interacting with the API ({msg})") 35 except Exception: 36 super().__init__( 37 "Error interacting with the API (http:" 38 f" {response.status_code})" 39 )