Commit adaf6193c01de495f2b77595268c6796d7beb8f7

Authored by selurvedu
1 parent 55b3f927

Handle IOError caused by nonexisting file

Calling log_object_data(..., update=True) caused an error
when there was no file to update.
@@ -117,12 +117,17 @@ def log_object_data(data, file_name=None, format="yaml", update=False): @@ -117,12 +117,17 @@ def log_object_data(data, file_name=None, format="yaml", update=False):
117 output_dir = BuiltIn().get_variable_value("${OUTPUT_DIR}") 117 output_dir = BuiltIn().get_variable_value("${OUTPUT_DIR}")
118 file_path = os.path.join(output_dir, file_name + '.' + format) 118 file_path = os.path.join(output_dir, file_name + '.' + format)
119 if update: 119 if update:
120 - with open(file_path, "r+") as file_obj:  
121 - new_data = data.copy()  
122 - data = munch_from_object(file_obj.read(), format)  
123 - data.update(new_data)  
124 - file_obj.seek(0)  
125 - file_obj.truncate() 120 + try:
  121 + with open(file_path, "r+") as file_obj:
  122 + new_data = data.copy()
  123 + data = munch_from_object(file_obj.read(), format)
  124 + data.update(new_data)
  125 + file_obj.seek(0)
  126 + file_obj.truncate()
  127 + except IOError as e:
  128 + LOGGER.log_message(Message(e, "INFO"))
  129 + LOGGER.log_message(Message("Nothing to update, "\
  130 + "creating new file.", "INFO"))
126 data_obj = munch_to_object(data, format) 131 data_obj = munch_to_object(data, format)
127 with open(file_path, "w") as file_obj: 132 with open(file_path, "w") as file_obj:
128 file_obj.write(data_obj) 133 file_obj.write(data_obj)
Please register or login to post a comment