1- from google .protobuf .json_format import Parse
1+ from google .protobuf .json_format import Parse , ParseError
22from synapse .api .device_pb2 import DeviceConfiguration
33from synapse .api .app_pb2 import AppManifest
44import 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