Skip to content

Commit 2a9eb8a

Browse files
authored
Handle invalid config file more gracefully (#567)
* Handle invalid config file more gracefully Fixes spurious build errors like: ``` Traceback (most recent call last): File "/snap/charm/461/bin/charm-build", line 11, in <module> load_entry_point('charm-tools==2.7.4', 'console_scripts', 'charm-build')() File "/snap/charm/461/lib/python3.6/site-packages/charmtools/build/builder.py", line 941, in main build() File "/snap/charm/461/lib/python3.6/site-packages/charmtools/build/builder.py", line 649, in __call__ self.generate() File "/snap/charm/461/lib/python3.6/site-packages/charmtools/build/builder.py", line 592, in generate layers = self.fetch() File "/snap/charm/461/lib/python3.6/site-packages/charmtools/build/builder.py", line 272, in fetch self.post_metrics('charm', self.name, False) File "/snap/charm/461/lib/python3.6/site-packages/charmtools/build/builder.py", line 529, in post_metrics cid = conf['cid'] TypeError: 'NoneType' object is not subscriptable ``` * Update per review
1 parent 9ce3206 commit 2a9eb8a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

charmtools/build/builder.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,15 @@ def post_metrics(self, kind, layer_name, fetched):
524524
if self.hide_metrics:
525525
return
526526
conf_file = path('~/.config/charm-build.conf').expanduser()
527-
if conf_file.exists():
528-
conf = yaml.safe_load(conf_file.text())
529-
cid = conf['cid']
530-
else:
527+
try:
528+
conf = yaml.safe_load(conf_file.text()) or {}
529+
except (FileNotFoundError, yaml.error.YAMLError):
530+
conf = {}
531+
if not conf.get('cid'):
531532
conf_file.parent.makedirs_p()
532-
cid = str(uuid.uuid4())
533-
conf_file.write_text(yaml.dump({'cid': cid}))
533+
conf['cid'] = str(uuid.uuid4())
534+
conf_file.write_text(yaml.safe_dump(conf))
535+
cid = conf['cid']
534536
try:
535537
requests.post(self.METRICS_URL, timeout=10, data={
536538
'tid': self.METRICS_ID,

0 commit comments

Comments
 (0)