Skip to content

Commit

Permalink
Add errors.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SeoulSKY committed Jul 1, 2024
1 parent a59b782 commit 32ac4e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ytnoti/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
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
from pyngrok import ngrok
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
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions ytnoti/errors.py
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit 32ac4e8

Please sign in to comment.