Skip to content

Commit 782d3bc

Browse files
calvinleng-scienceCalvin Leng
andauthored
Picked up the exceptions from protobuf JSON parser to give more informative config errors (#158)
Co-authored-by: Calvin Leng <calvinl@science.xyz>
1 parent cef4dc8 commit 782d3bc

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

synapse/utils/proto.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from google.protobuf.json_format import Parse
1+
from google.protobuf.json_format import Parse, ParseError
22
from synapse.api.device_pb2 import DeviceConfiguration
33
from synapse.api.app_pb2 import AppManifest
44
import synapse as syn
@@ -8,20 +8,35 @@ def load_device_config(path_to_json, console):
88
# We support either a manifest or a device configuration.
99
# First, try to load a device configuration
1010
try:
11-
json_text = open(path_to_json, "r").read()
11+
with open(path_to_json, "r") as f:
12+
json_text = f.read()
13+
except FileNotFoundError:
14+
raise ValueError(f"File not found: {path_to_json}")
15+
except PermissionError:
16+
raise ValueError(f"Permission denied reading: {path_to_json}")
17+
except Exception as e:
18+
raise ValueError(f"Failed to read {path_to_json}: {e}")
19+
20+
errors = []
21+
try:
1222
cfg_proto = Parse(json_text, DeviceConfiguration())
1323
return syn.Config.from_proto(cfg_proto)
14-
except Exception:
15-
pass
24+
except ParseError as e:
25+
errors.append(f"DeviceConfiguration: {str(e)}")
26+
except Exception as e:
27+
errors.append(f"DeviceConfiguration: {type(e).__name__}: {str(e)}")
1628

1729
# We couldn't load a device configuration, so try to load a manifest
1830
try:
1931
json_text = open(path_to_json, "r").read()
2032
manifest_proto = Parse(json_text, AppManifest())
2133
return syn.Config.from_proto(manifest_proto.device_config)
34+
except ParseError as e:
35+
errors.append(f"AppManifest: {str(e)}")
2236
except Exception:
23-
raise ValueError(
24-
f"Could not parse {path_to_json} as either device configuration or manifest"
25-
)
37+
errors.append(f"AppManifest: {type(e).__name__}: {str(e)}")
2638

27-
return None
39+
# Only reached here when we've failed to parse
40+
error_msg = f"Could not parse {path_to_json} as either format:\n"
41+
error_msg += "\n".join(f" - {error}" for error in errors)
42+
raise ValueError(error_msg)

0 commit comments

Comments
 (0)