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

Custom error handling #31

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
53 changes: 51 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fastapi = "^0.110.0"
starlette = "^0.36.3"
uvicorn = "^0.29.0"
keylimiter = "^0.1.5"
loguru = "^0.7.2"


[tool.poetry.group.dev]
Expand Down
22 changes: 11 additions & 11 deletions src/communex/cli/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import communex.balance as c_balance
from communex.compat.key import classic_load_key
from communex.errors import ChainTransactionError
from communex.errors import ChainTransactionError, InvalidIPError, InvalidClassError, InvalidModuleError
from communex.misc import get_map_modules
from communex.module.server import ModuleServer
from communex.util import is_ip_valid
Expand Down Expand Up @@ -45,7 +45,7 @@ def register(
resolved_key = classic_load_key(key)

if not is_ip_valid(ip):
raise ValueError("Invalid ip address")
raise InvalidIPError("Invalid ip address")

address = f"{ip}:{port}"
subnet = client.get_name(netuid)
Expand All @@ -70,7 +70,7 @@ def update(key: str, name: str, ip: str, port: int, delegation_fee: int = 20, ne
client = make_client()

if not is_ip_valid(ip):
raise ValueError("Invalid ip address")
raise InvalidIPError("Invalid ip address")

address = f"{ip}:{port}"

Expand Down Expand Up @@ -107,24 +107,24 @@ def serve(
module_path = ".".join(module_parts)
if not module_path:
# This could do some kind of relative import somehow?
raise ValueError(f"Invalid class path: `{class_path}`, module name is missing")
raise InvalidModuleError(f"Invalid class path: `{class_path}`, module name is missing")
if not class_name:
raise ValueError(f"Invalid class path: `{class_path}`, class name is missing")
raise InvalidClassError(f"Invalid class path: `{class_path}`, class name is missing")
case _:
# This is impossible
raise Exception(f"Invalid class path: `{class_path}`")
raise TypeError(f"Invalid class path: `{class_path}`")

try:
module = importlib.import_module(module_path)
except ModuleNotFoundError:
except ModuleNotFoundError as e:
context.error(f"Module `{module_path}` not found")
raise typer.Exit(code=1)
raise typer.Exit(code=1) from e

try:
class_obj = getattr(module, class_name)
except AttributeError:
except AttributeError as e:
context.error(f"Class `{class_name}` not found in module `{module}`")
raise typer.Exit(code=1)
raise typer.Exit(code=1) from e

keypair = classic_load_key(key)
server = ModuleServer(class_obj(), keypair, whitelist=whitelist,
Expand All @@ -150,7 +150,7 @@ def info(name: str, balance: bool = False, netuid: int = 0):
module = next((item for item in modules_to_list if item["name"] == name), None)

if module is None:
raise ValueError("Module not found")
raise InvalidModuleError("Module not found")

general_module = cast(dict[str, Any], module)
print_table_from_plain_dict(general_module, ["Params", "Values"], console)
Expand Down
28 changes: 15 additions & 13 deletions src/communex/cli/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,21 @@ def propose_globally(

resolved_key = classic_load_key(key)

proposal: NetworkParams = {"max_allowed_subnets": max_allowed_subnets,
"max_allowed_modules": max_allowed_modules,
"max_registrations_per_block": max_registrations_per_block,
"unit_emission": unit_emission,
"tx_rate_limit": tx_rate_limit,
"vote_threshold": vote_threshold,
"vote_mode": vote_mode,
"max_proposals": max_proposals,
"max_name_length": max_name_length,
"burn_rate": burn_rate,
"min_burn": min_burn,
"min_stake": min_stake,
"min_weight_stake": min_weight_stake}
proposal = NetworkParams(
max_allowed_subnets=max_allowed_subnets,
max_allowed_modules=max_allowed_modules,
max_registrations_per_block=max_registrations_per_block,
unit_emission=unit_emission,
tx_rate_limit=tx_rate_limit,
vote_threshold=vote_threshold,
vote_mode=vote_mode,
max_proposals=max_proposals,
max_name_length=max_name_length,
burn_rate=burn_rate,
min_burn=min_burn,
min_stake=min_stake,
min_weight_stake=min_weight_stake
)

with console.status("Adding a proposal..."):
client.add_global_proposal(resolved_key, proposal)
Expand Down
Loading