From 32ac4e808bcdf72f48a8dffb237e5f5bdfe8cb5c Mon Sep 17 00:00:00 2001 From: SeoulSKY Date: Mon, 1 Jul 2024 10:06:28 -0600 Subject: [PATCH] Add errors.py --- ytnoti/base.py | 5 +++-- ytnoti/errors.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 ytnoti/errors.py diff --git a/ytnoti/base.py b/ytnoti/base.py index 5da3adf..24f5db1 100644 --- a/ytnoti/base.py +++ b/ytnoti/base.py @@ -14,7 +14,7 @@ import hmac from urllib.parse import urlparse -from httpx import AsyncClient, HTTPError +from httpx import AsyncClient import xmltodict from fastapi import FastAPI, Request, Response, APIRouter from uvicorn import Config, Server @@ -22,6 +22,7 @@ from pyexpat import ExpatError from ytnoti.enums import NotificationKind, ServerMode +from ytnoti.errors import HTTPError from ytnoti.models import YouTubeNotifierConfig from ytnoti.models.history import InMemoryVideoHistory, VideoHistory from ytnoti.models.video import Channel, Thumbnail, Video, Stats, Timestamp @@ -400,7 +401,7 @@ async def _register(self, raise ConnectionError(f"Cannot {mode} while the server is not listening") if response.status_code != HTTPStatus.NO_CONTENT.value: - raise HTTPError(f"Failed to {mode} channel ({channel_id}) with status code {response.status_code}") + raise HTTPError(f"Failed to {mode} channel: {channel_id}", response.status_code) self.__logger.info("Successfully %sd channel: %s", mode, channel_id) diff --git a/ytnoti/errors.py b/ytnoti/errors.py new file mode 100644 index 0000000..7e385db --- /dev/null +++ b/ytnoti/errors.py @@ -0,0 +1,24 @@ +""" +This module contains custom exceptions for the ytnoti package. +""" + +from http import HTTPStatus + + +class HTTPError(Exception): + """ + Exception raised when an HTTP error occurs. + """ + + def __init__(self, message: str, status_code: int | HTTPStatus): + """ + Initialize the HTTPError object. + + :param message: The error message + :param status_code: The status code of the error + """ + self.status_code = status_code if isinstance(status_code, HTTPStatus) else HTTPStatus(status_code) + self.message = message + + def __str__(self): + return f"Status code: {self.status_code}: {self.message}"