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

Commit

Permalink
Improve error handling of account init with invalid key (#158)
Browse files Browse the repository at this point in the history
* add try/except for signer

* fix msg

* fix test
  • Loading branch information
andrew-fleming authored Aug 4, 2022
1 parent 0f696ea commit 1d1bc85
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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

0 comments on commit 1d1bc85

Please sign in to comment.