diff --git a/tubot/dirgetter/getter.py b/tubot/dirgetter/getter.py index 59338b9..253aeae 100644 --- a/tubot/dirgetter/getter.py +++ b/tubot/dirgetter/getter.py @@ -32,7 +32,7 @@ class OSGetter(DirGetter): dirs = {} ls = await listdir(self.base_dir) if len(ls) == 0: - raise ValueError("No dirs found") + raise KeyError("No dirs found") for item in ls: if await isdir(f"{self.base_dir}/{item}"): dirs[item] = f"{self.base_dir}/{item}" diff --git a/tubot/static/controller.py b/tubot/static/controller.py index c4fd20f..7834fbe 100644 --- a/tubot/static/controller.py +++ b/tubot/static/controller.py @@ -11,6 +11,7 @@ from tubot.db.abc import CacheDB from tubot.db.types import User, UserStates from tubot.static.functions import validate from tubot.static.env import AUTH_PASSWD +from tubot.static.exceptions import AlreadyExists, AuthError class Controller(object): @@ -44,7 +45,7 @@ class Controller(object): dirs = await self.getter.folders if len(dirs) > 0: return dirs - raise ValueError("No dirs found") + raise KeyError("No dirs found") # Torrent @@ -54,7 +55,7 @@ class Controller(object): :param torrent: Torrent object """ - await validate(torrent) + await validate(torrent, "Wrong torrent object") await self.torrent.upload(torrent) async def get_torrent_list(self) -> str: @@ -102,11 +103,11 @@ class Controller(object): :param user: Current user object """ if user.auth: - raise ValueError("Already auth") + raise AlreadyExists("You already auth") if pwd == AUTH_PASSWD()(): user.auth = True await self.cache.write_user(user.tg_id, user) - raise ValueError("Wrong password") + raise AuthError("Wrong password") async def set_user_state(self, user: User, state: UserStates) -> None: """ diff --git a/tubot/static/exceptions.py b/tubot/static/exceptions.py new file mode 100644 index 0000000..ac311d6 --- /dev/null +++ b/tubot/static/exceptions.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- + +################## +# ExceptionTypes # +################## + + +class ValidationError(Exception): + """ + Validation error exception + """ + + +class AuthError(Exception): + """ + Authentification error exception + """ + + +class AlreadyExists(Exception): + """ + Object already exists error exception + """ diff --git a/tubot/static/functions.py b/tubot/static/functions.py index 3b826e5..9749a9b 100644 --- a/tubot/static/functions.py +++ b/tubot/static/functions.py @@ -6,6 +6,7 @@ # Imports from tubot.static.abc import IValidatable +from tubot.static.exceptions import ValidationError async def validate(obj: IValidatable, msg: str | None = None) -> bool: @@ -17,5 +18,5 @@ async def validate(obj: IValidatable, msg: str | None = None) -> bool: if await obj.__validate__(): return True if msg is None: - raise TypeError("Object validation failed") - raise TypeError(f"Object validation failed: {msg}") + raise ValidationError("Object validation failed") + raise ValidationError(f"Object validation failed: {msg}") diff --git a/tubot/static/init.py b/tubot/static/init.py index 25f1a90..65553fa 100644 --- a/tubot/static/init.py +++ b/tubot/static/init.py @@ -50,7 +50,7 @@ class InitBuilder(object): self.TORRENT_SERVER = qBitTorrent(host, user, pwd) case _: raise TypeError - await validate(self.TORRENT_SERVER) + await validate(self.TORRENT_SERVER, "TorrentServerAPI validation error") async def init_dg(self) -> None: match self.DG: @@ -63,7 +63,7 @@ class InitBuilder(object): self.DG = Jellyfin(host, key) case _: raise TypeError - await validate(self.DG) + await validate(self.DG, "DirectoryGetter validation error") async def init_cache(self) -> None: match self.CACHE: @@ -74,7 +74,7 @@ class InitBuilder(object): self.CACHE = RedisCache(host) case _: raise TypeError - await validate(self.CACHE) + await validate(self.CACHE, "CacheDataBase validation error") async def init_all_modules(self) -> None: tasks = ( diff --git a/tubot/torrent/apis.py b/tubot/torrent/apis.py index 724a0cb..4965d4f 100644 --- a/tubot/torrent/apis.py +++ b/tubot/torrent/apis.py @@ -11,6 +11,7 @@ from tubot.torrent.abc import TorrentAPI from tubot.torrent.torrents import TorrentFile, TorrentMagnet, TorrentURL from tubot.torrent.types import ServerTypes, TorrentListBuilder from tubot.static.functions import validate +from tubot.static.exceptions import AuthError class qBitTorrent(TorrentAPI): @@ -77,7 +78,7 @@ class qBitTorrent(TorrentAPI): cookies = resp.cookies resp = await self._get(api="api/v2/app/version", cookie=cookies) if resp.status != 200: - raise ValueError("Auth error") + raise AuthError("Wrong creds") self.cookie = cookies return True except Exception: @@ -85,7 +86,7 @@ class qBitTorrent(TorrentAPI): return False async def upload_file(self, torrent: TorrentFile) -> None: - await validate(self) + await validate(self, "Connection to TorrentServer failed") await validate(torrent, "Bad .torrent file") bytes = await torrent.getbytes() data = FormData() @@ -99,20 +100,20 @@ class qBitTorrent(TorrentAPI): await self._post("api/v2/torrents/add", cookie=self.cookie, data=data) async def upload_magnet(self, torrent: TorrentMagnet) -> None: - await validate(self) + await validate(self, "Connection to TorrentServer failed") await validate(torrent, "Bad magnet link") data = {"urls": torrent.content, "savepath": torrent.dest} await self._post("api/v2/torrents/add", cookie=self.cookie, data=data) async def upload_url(self, torrent: TorrentURL) -> None: - await validate(self) + await validate(self, "Connection to TorrentServer failed") await validate(torrent, "Bad url") data = {"urls": torrent.content, "savepath": torrent.dest} await self._post("api/v2/torrents/add", cookie=self.cookie, data=data) @property async def torrent_list(self) -> str: - await validate(self) + await validate(self, "Connection to TorrentServer failed") responce = await self._get( "api/v2/torrents/info?filter=completed,downloading&sort=progress", cookie=self.cookie,