Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Improve error handling of account init with invalid key #158

Merged
merged 3 commits into from
Aug 4, 2022
Merged
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
13 changes: 11 additions & 2 deletions src/nile/core/account.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Command to call or invoke StarkNet smart contracts."""
import logging
import os

from dotenv import load_dotenv
Expand All @@ -20,8 +21,16 @@ class Account:

def __init__(self, signer, network):
"""Get or deploy an Account contract for the given private key."""
self.signer = Signer(int(os.environ[signer]))
self.network = network
try:
self.signer = Signer(int(os.environ[signer]))
self.network = network
except KeyError:
logging.error(
f"\n❌ Cannot find {signer} in env."
"\nCheck spelling and that it exists."
"\nTry moving the .env to the root of your project."
)
return

if accounts.exists(str(self.signer.public_key), network):
signer_data = next(accounts.load(str(self.signer.public_key), network))
Expand Down
12 changes: 12 additions & 0 deletions tests/commands/test_account.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for account commands."""
import logging
from unittest.mock import MagicMock, patch

import pytest
Expand Down Expand Up @@ -27,6 +28,17 @@ def test_account_init(mock_deploy):
mock_deploy.assert_called_once()


def test_account_init_bad_key(caplog):
logging.getLogger().setLevel(logging.INFO)

Account("BAD_KEY", NETWORK)
assert (
"\n❌ Cannot find BAD_KEY in env."
"\nCheck spelling and that it exists."
"\nTry moving the .env to the root of your project."
) in caplog.text


def test_account_multiple_inits_with_same_key():
account = Account(KEY, NETWORK)
account.deploy()
Expand Down