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

Making the errors more specific and checking the address for typing errors #1

Open
wants to merge 2 commits 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
44 changes: 27 additions & 17 deletions execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def register_particle(addr):
"""This function inits the particle."""
url = f"{node_url}/register_particle"
response = requests.post(url, timeout=10, json={"address": addr})
if response.status_code != 200:
if response.status_code == 500:
raise Exception(f"No task available: Try later.")
elif response.status_code != 200:
raise Exception(f"Failed to init particle: Try later.")
task = response.json()
return task['args']
Expand All @@ -96,23 +98,31 @@ def complete_task(wallet_address):


def perform():

#Checks if the address is provided
if (len(sys.argv) < 2) or (addr is None):
print_in_color("Error: Address not provided.", "\033[31m")
return
addr = sys.argv[1]
if addr is not None:
print_in_color(f"Address {addr} started to work.", "\033[33m")
while True:
try:
print_in_color(f"Preparing", "\033[33m")
time.sleep(10)
task_args = register_particle(addr)
print_in_color(f"Address {addr} received the task.", "\033[33m")
execute(task_args)
print_in_color(f"Address {addr} executed the task.", "\033[32m")
complete_task(addr)
print_in_color(f"Address {addr} completed the task. ", "\033[32m")
except Exception as e:
print_in_color(f"Error: {e}", "\033[31m")
else:
print_in_color("Address not provided.", "\033[31m")

#Checks if the addr starts with nimble1 and is 45 characters long
if (not addr.startswith("nimble1")) or (len(addr) != 45):
print_in_color("Error: Invalid address.", "\033[31m")
return

print_in_color(f"Address {addr} started to work.", "\033[33m")
while True:
try:
print_in_color(f"Preparing", "\033[33m")
time.sleep(10)
task_args = register_particle(addr)
print_in_color(f"Address {addr} received the task.", "\033[33m")
execute(task_args)
print_in_color(f"Address {addr} executed the task.", "\033[32m")
complete_task(addr)
print_in_color(f"Address {addr} completed the task. ", "\033[32m")
except Exception as e:
print_in_color(f"Error: {e}", "\033[31m")

if __name__ == "__main__":
perform()