Skip to content

Commit 21b4397

Browse files
committed
Adding exception handling.
client_from_file will now catch errors if the config file is missing a required parameter for the ox3apiclient.Client instance.
1 parent 0e80d48 commit 21b4397

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

ox3apiclient/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,13 @@ def client_from_file(file_path='.ox3rc', env=None):
237237

238238
client_params = {}
239239

240-
# TODO: Catch NoOptionErrors.
241-
for key in required_params:
242-
client_params[key] = cp.get(env, key)
240+
# Load required parameters.
241+
try:
242+
for key in required_params:
243+
client_params[key] = cp.get(env, key)
244+
except ConfigParser.NoOptionError:
245+
err_msg = "Missing required option: '%s'" % key
246+
raise Exception(err_msg)
243247

244248
# TODO: Add support for optional parameters.
245249

tests/clientfromfile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ def test_loads_alternate_env(self):
5151
test_values.sort()
5252
loaded_values.sort()
5353
self.assertEqual(loaded_values, test_values)
54+
55+
def test_missing_required_option_raises_error(self):
56+
file_path = os.path.join(os.path.dirname(__file__), 'ox3rctest')
57+
self.assertRaises(
58+
Exception,
59+
ox3apiclient.client_from_file,
60+
file_path,
61+
'missing-required-option')

tests/ox3rctest

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
envs=
33
prod
44
dev
5+
missing-required-option
56

67
[prod]
78
email: email
@@ -18,3 +19,10 @@ domain: domain_dev
1819
realm: realm_dev
1920
consumer_key: consumer_key_dev
2021
consumer_secret: consumer_secret_dev
22+
23+
[missing-required-option]
24+
email: email
25+
password: password
26+
domain: domain
27+
realm: realm
28+
consumer_key: consumer_key

0 commit comments

Comments
 (0)