Reformat exceptions

- init static.exceptions.py
- reformat exceptions
- add static.functions.validate() messages
This commit is contained in:
trueold89 2024-08-07 19:49:55 +03:00
parent eb1dc48a11
commit f88f007910
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
6 changed files with 41 additions and 15 deletions

View File

@ -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}"

View File

@ -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:
"""

View File

@ -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
"""

View File

@ -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}")

View File

@ -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 = (

View File

@ -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,