improve logging/exceptions for data reception, new exceptions.py file

This commit is contained in:
Xevion
2021-01-22 09:39:16 -06:00
parent 12917edaa4
commit 53faae4154
2 changed files with 23 additions and 6 deletions

5
exceptions.py Normal file
View File

@@ -0,0 +1,5 @@
class TCPChatException(BaseException):
pass
class DataReceptionException(TCPChatException):
pass

View File

@@ -4,11 +4,13 @@ import random
import socket
import time
import uuid
from typing import Any, List
from json import JSONDecodeError
from typing import Any, List, Optional
import constants
import helpers
# noinspection PyUnresolvedReferences
from exceptions import DataReceptionException
from server import db
from server.commands import CommandHandler
from server.db import Database
@@ -123,11 +125,21 @@ class Client(BaseClient):
cur.close()
def receive(self) -> Any:
length = int(self.conn.recv(constants.HEADER_LENGTH).decode('utf-8'))
logger.debug(f'Header received - Length {length}')
data = json.loads(self.conn.recv(length).decode('utf-8'))
logger.info(f'Data received/parsed, type: {data["type"]}')
return data
try:
length = int(self.conn.recv(constants.HEADER_LENGTH).decode('utf-8'))
except ValueError:
raise DataReceptionException('The socket did not receive the expected header.')
else:
logger.debug(f'Header received - Length {length}')
try:
data = self.conn.recv(length).decode('utf-8')
data = json.loads(data)
except JSONDecodeError:
raise DataReceptionException('The socket received a invalid JSON structure.')
else:
logger.info(f'Data received/parsed, type: {data["type"]}')
return data
def handle_nickname(self, nickname: str) -> None:
if self.last_nickname_change is None: