Skip to content

Commit 49658c5

Browse files
committed
Add exit codes for errors
Resolves #10 Signed-off-by: Gregers Gram Rygg <gregers.gram.rygg@nordicsemi.no>
1 parent 9fa106d commit 49658c5

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

src/nrfcredstore/cli.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
KEY_TYPES = KEY_TYPES_OR_ANY.copy()
1212
KEY_TYPES.remove('ANY')
1313

14+
ERR_UNKNOWN = 1
15+
ERR_NO_AT_CLIENT = 10
16+
ERR_AT_COMMAND = 11
17+
ERR_TIMEOUT = 12
18+
ERR_SERIAL = 13
1419

1520
def parse_args(in_args):
1621
parser = argparse.ArgumentParser(description='Manage certificates stored in a cellular modem.')
@@ -89,6 +94,10 @@ def exec_cmd(args, credstore):
8994
print(f'New private key generated in secure tag {args.tag}')
9095
print(f'Wrote CSR in DER format to {args.file.name}')
9196

97+
def exit_with_msg(exitcode, msg):
98+
print(msg)
99+
exit(exitcode)
100+
92101
def main(in_args, credstore):
93102
at_client = credstore.at_client
94103
try:
@@ -98,14 +107,16 @@ def main(in_args, credstore):
98107
at_client.verify()
99108
at_client.enable_error_codes()
100109
exec_cmd(args, credstore)
101-
except serial.SerialException as err:
102-
print(f'Serial error: {err}')
103110
except NoATClientException:
104-
print('The device does not respond to AT commands. Please flash at_client sample.')
111+
exit_with_msg(ERR_NO_AT_CLIENT, 'The device does not respond to AT commands. Please flash at_client sample.')
105112
except ATCommandError as err:
106-
print(err)
113+
exit_with_msg(ERR_AT_COMMAND, err)
107114
except TimeoutError as err:
108-
print('The device did not respond in time. Please try again.')
115+
exit_with_msg(ERR_TIMEOUT, 'The device did not respond in time. Please try again.')
116+
except serial.SerialException as err:
117+
exit_with_msg(ERR_SERIAL, f'Serial error: {err}')
118+
except Exception as err:
119+
exit_with_msg(ERR_UNKNOWN, f'Unhandled Error: {err}')
109120

110121
def run():
111122
main(sys.argv[1:], CredStore(ATClient(serial.Serial())))

tests/test_cli.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import pytest
22

33
from unittest.mock import Mock, ANY, patch
4+
from serial import SerialException
45
from nrfcredstore.cli import main
56

67
from nrfcredstore.credstore import CredType
7-
from nrfcredstore.exceptions import NoATClientException
8+
from nrfcredstore.exceptions import NoATClientException, ATCommandError
89

910
# pylint: disable=no-self-use
1011
class TestCli():
@@ -47,7 +48,8 @@ def test_at_client_verify(self, credstore, at_client, offline, empty_cred_list):
4748
@patch('builtins.print')
4849
def test_at_client_verify_fail(self, mock_print, credstore, at_client):
4950
at_client.verify.side_effect = NoATClientException()
50-
main(['fakedev', 'list'], credstore)
51+
with pytest.raises(SystemExit) as e:
52+
main(['fakedev', 'list'], credstore)
5153
assert 'does not respond to AT commands' in mock_print.call_args[0][0]
5254

5355
def test_at_client_enable_error_codes(self, credstore, at_client, offline, empty_cred_list):
@@ -103,3 +105,38 @@ def test_generate_file(self, mock_file, credstore, offline):
103105
credstore.keygen.return_value = True
104106
main(['fakedev', 'generate', '123', 'foo.der'], credstore)
105107
mock_file.assert_called_with('foo.der', 'wb', ANY, ANY, ANY)
108+
109+
def test_no_at_client_exit_code(self, credstore, at_client):
110+
at_client.verify.side_effect = NoATClientException()
111+
with pytest.raises(SystemExit) as e:
112+
main(['fakedev', 'list'], credstore)
113+
assert e.type == SystemExit
114+
assert e.value.code == 10
115+
116+
def test_at_command_error_exit_code(self, credstore, at_client):
117+
at_client.verify.side_effect = ATCommandError()
118+
with pytest.raises(SystemExit) as e:
119+
main(['fakedev', 'list'], credstore)
120+
assert e.type == SystemExit
121+
assert e.value.code == 11
122+
123+
def test_timeout_error_exit_code(self, credstore, at_client):
124+
at_client.verify.side_effect = TimeoutError()
125+
with pytest.raises(SystemExit) as e:
126+
main(['fakedev', 'list'], credstore)
127+
assert e.type == SystemExit
128+
assert e.value.code == 12
129+
130+
def test_serial_exception_exit_code(self, credstore, at_client):
131+
at_client.connect.side_effect = SerialException()
132+
with pytest.raises(SystemExit) as e:
133+
main(['fakedev', 'list'], credstore)
134+
assert e.type == SystemExit
135+
assert e.value.code == 13
136+
137+
def test_unhandled_exception_exit_code(self, credstore, at_client):
138+
at_client.verify.side_effect = Exception()
139+
with pytest.raises(SystemExit) as e:
140+
main(['fakedev', 'list'], credstore)
141+
assert e.type == SystemExit
142+
assert e.value.code == 1

0 commit comments

Comments
 (0)