Add error management in important functions

This commit is contained in:
Adrien Ufferte
2025-08-12 12:35:33 +02:00
parent 781105b832
commit b9cbccab5e
10 changed files with 92 additions and 4 deletions
+7
View File
@@ -27,6 +27,13 @@ func extract(zip_path: String, extract_path: String, extract_in_subfolder: bool
if not DirAccess.dir_exists_absolute(folder_name):
DirAccess.make_dir_recursive_absolute(folder_name)
var file: FileAccess = FileAccess.open(file_name, FileAccess.WRITE)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("FolderUnzipper: Extract: Cannot open file %s. Error: %s" % [file_name, error_string(error)])
return first_folder
if file == null:
Logger.error("FolderUnzipper: Extract: Cannot open file %s. File is null" % file_name)
return first_folder
if file != null:
file.store_buffer(read_file(sub_path))
file.close()
+4
View File
@@ -27,6 +27,10 @@ func write_folder_recursive(abs_path: String, rel_path: String) -> Error:
return error
var file: FileAccess = FileAccess.open(current_full_path, FileAccess.READ)
error = DirAccess.get_open_error()
if error != OK:
Logger.error("FolderZipper: Extract: Cannot open file %s. Error: %s" % [file_name, error_string(error)])
return error
if file:
write_file(file.get_buffer(file.get_length()))
file.close()
+7
View File
@@ -122,6 +122,13 @@ func _on_list_title_save_pressed() -> void:
func _on_list_title_import_path_selected(path: String, match_to_file: bool) -> void:
var file: FileAccess = FileAccess.open(path, FileAccess.READ)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("GPList: Cannot open file %s. Error: %s" % [path, error_string(error)])
return
if file == null:
Logger.error("GPList: Cannot open file %s. File is null" % path)
return
var line: PackedStringArray = file.get_csv_line()
if line.size() < 4 or line[0] != "Grapheme" or line[1] != "Phoneme" or line[2] != "Type" or line[3] != "Exception":
error_label.text = "Column names should be Grapheme, Phoneme, Type, Exception"
@@ -49,6 +49,13 @@ func set_sound_preview(sound_path: String) -> void:
sound_preview.hide()
else:
var file: FileAccess = FileAccess.open(sound_path, FileAccess.READ)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("ImageAndSoundGPDescription: Cannot open file %s. Error: %s" % [sound_path, error_string(error)])
return
if file == null:
Logger.error("ImageAndSoundGPDescription: Cannot open file %s. File is null" % sound_path)
return
var sound: AudioStreamMP3 = AudioStreamMP3.new()
sound.data = file.get_buffer(file.get_length())
sound_player.stream = sound
+14
View File
@@ -59,6 +59,13 @@ func _load_segments(segment_container: SegmentContainer, path: String) -> void:
return
var file: FileAccess = FileAccess.open(real_path(path), FileAccess.READ)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("TracingBuilder: Load segment: Cannot open file %s. Error: %s" % [real_path(path), error_string(error)])
return
if file == null:
Logger.error("TracingBuilder: Load segment: Cannot open file %s. File is null" % real_path(path))
return
while not file.eof_reached():
var line: PackedStringArray = file.get_csv_line()
var points: Array[Vector2] = []
@@ -75,6 +82,13 @@ func _load_segments(segment_container: SegmentContainer, path: String) -> void:
func _save_segments(segments: Array[SegmentBuild], path: String) -> void:
DirAccess.make_dir_recursive_absolute(Database.BASE_PATH.path_join(Database.language).path_join(Database.TRACING_DATA_FOLDER))
var file: FileAccess = FileAccess.open(real_path(path), FileAccess.WRITE)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("TracingBuilder: Save segment: Cannot open file %s. Error: %s" % [real_path(path), error_string(error)])
return
if file == null:
Logger.error("TracingBuilder: Save segment: Cannot open file %s. File is null" % real_path(path))
return
for segment: SegmentBuild in segments:
var values: PackedStringArray = []
for point: Vector2 in segment.points:
+11
View File
@@ -227,7 +227,18 @@ func _on_word_gui_input(event: InputEvent) -> void:
func _on_list_title_import_path_selected(path: String, match_to_file: bool) -> void:
if not FileAccess.file_exists(path):
Logger.error("WordList: File not found %s" % path)
error_label.text = "File not found"
return
var file: FileAccess = FileAccess.open(path, FileAccess.READ)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("WordList: Cannot open file %s. Error: %s" % [path, error_string(error)])
return
if file == null:
Logger.error("WordList: Cannot open file %s. File is null" % path)
return
var line: PackedStringArray = file.get_csv_line()
if line.size() < 2 or line[0] != "ORTHO" or line[1] != "GPMATCH":
error_label.text = "Column names should be ORTHO, GPMATCH"
@@ -83,6 +83,13 @@ func _load_tracing(path: String) -> Array:
var segments: Array = []
var file: FileAccess = FileAccess.open(_real_path(path), FileAccess.READ)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("TracingManager: Load tracing: Cannot open file %s. Error: %s" % [_real_path(path), error_string(error)])
return segments
if file == null:
Logger.error("TracingManager: Load tracing: Cannot open file %s. File is null" % _real_path(path))
return segments
while not file.eof_reached():
var points: Array[Vector2] = []
var line: PackedStringArray = file.get_csv_line()
@@ -165,6 +165,9 @@ func _copy_data(this: PackageDownloader) -> void:
# Extract the archive
var subfolder: String = unzipper.extract(language_zip_path, USER_LANGUAGE_RESOURCES_PATH, false)
if subfolder == "":
Logger.error("PackageDownloader: Extraction failed for %s" % language_zip_path)
return
# Move the data to the locale folder of the user
var error: Error = DirAccess.rename_absolute(USER_LANGUAGE_RESOURCES_PATH.path_join(subfolder), current_language_path)
+21
View File
@@ -64,6 +64,13 @@ func load_additional_word_list() -> String:
var word_list_path: String = get_additional_word_list_path()
if FileAccess.file_exists(word_list_path):
var file: FileAccess = FileAccess.open(word_list_path, FileAccess.READ)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("Database: Load additional word list: Cannot open file %s. Error: %s" % [word_list_path, error_string(error)])
return ""
if file == null:
Logger.error("Database: Load additional word list: Cannot open file %s. File is null" % word_list_path)
return ""
var title_line: PackedStringArray = file.get_csv_line()
if (not "ORTHO" in title_line) or (not "PHON" in title_line) or (not "GPMATCH" in title_line):
var msg: String = "word list should have columns ORTHO, PHON and GPMATCH"
@@ -517,6 +524,13 @@ func get_gp_look_and_learn_sound(gp: Dictionary) -> AudioStream:
return load(path)
else:
var file: FileAccess = FileAccess.open(path, FileAccess.READ)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("Database: Get GP look and learn sound: Cannot open file %s. Error: %s" % [path, error_string(error)])
return null
if file == null:
Logger.error("Database: Get GP look and learn sound: Cannot open file %s. File is null" % path)
return null
var sound: AudioStreamMP3 = AudioStreamMP3.new()
sound.data = file.get_buffer(file.get_length())
return sound
@@ -580,6 +594,13 @@ func load_external_sound(path: String) -> AudioStreamMP3:
return null
var file: FileAccess = FileAccess.open(path, FileAccess.READ)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("Database: Load external sound: Cannot open file %s. Error: %s" % [path, error_string(error)])
return null
if file == null:
Logger.error("Database: Load external sound: Cannot open file %s. File is null" % path)
return null
var audio_stream: AudioStreamMP3 = AudioStreamMP3.new()
audio_stream.data = file.get_buffer(file.get_length())
file.close()
@@ -166,6 +166,13 @@ func safe_load_and_fix_resource(path: String, old_texts: Array[String], new_text
Logger.info("UserDataManager: Fix resource:" + path)
content = content.replace(old_texts[index], new_texts[index])
var file: FileAccess = FileAccess.open(path, FileAccess.WRITE)
var error: Error = DirAccess.get_open_error()
if error != OK:
Logger.error("UserDataManager: Safe load and fix resource: Cannot open file %s. Error: %s" % [path, error_string(error)])
return null
if file == null:
Logger.error("UserDataManager: Safe load and fix resource: Cannot open file %s. File is null" % path)
return null
file.store_string(content)
file.close()