Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amqp: add try catch #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions amqp/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ def start(self):
def _handle_client(self, client: socket.socket):
with client:
# Receive key
key = client.recv(1024).decode()
print(key)
print(type(key))
try:
key = client.recv(1024).decode()
print(key)
print(type(key))
except Exception as e:
print("Error receiving key: {}".format(e))
client.sendall("Error receiving key: {}\nClosing your connection".format(e).encode())
return
# Bind client to exchange
try:
self._exchange.bind(key, client)
Expand All @@ -66,8 +71,16 @@ def _handle_client(self, client: socket.socket):
print("Closing connection to client: {}".format(client))
break
# Parse message as json
message = base64.b64decode(message)
msg_json = json.loads(message)
try:
message = base64.b64decode(message)
except:
client.sendall(b"Message is not properly base64 encoded")
continue
try:
msg_json = json.loads(message)
except:
client.sendall(b"Message is not valid json")
continue
# Check if message has routing key
if 'key' not in msg_json:
client.sendall(b"Message has no routing key")
Expand Down