Skip to content

Commit

Permalink
Improve error handling and logs (#165)
Browse files Browse the repository at this point in the history
- Do not use one-word variable for searchability
- Add literal log message with error message for searchability
- Do not bundle multiple throws with one catch
  • Loading branch information
ryonakano committed Mar 3, 2024
1 parent 259c1c4 commit 88a25e8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
58 changes: 29 additions & 29 deletions src/Model/DesktopFile.vala
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public class Model.DesktopFile : Object {

try {
val = keyfile.get_boolean (KeyFileDesktop.GROUP, key);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.get_boolean: key=%s: %s", key, e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.get_boolean: key=%s: %s", key, err.message);
}

return val;
Expand All @@ -106,8 +106,8 @@ public class Model.DesktopFile : Object {

try {
val = keyfile.get_string (KeyFileDesktop.GROUP, key);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.get_string: key=%s: %s", key, e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.get_string: key=%s: %s", key, err.message);
}

return val;
Expand All @@ -118,8 +118,8 @@ public class Model.DesktopFile : Object {

try {
ret = keyfile.has_key (KeyFileDesktop.GROUP, key);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.has_key: key=%s: %s", key, e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.has_key: key=%s: %s", key, err.message);
}

return ret;
Expand All @@ -142,8 +142,8 @@ public class Model.DesktopFile : Object {

try {
val = keyfile.get_string_list (KeyFileDesktop.GROUP, key);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.get_string_list: key=%s: %s", key, e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.get_string_list: key=%s: %s", key, err.message);
}

return val;
Expand All @@ -158,8 +158,8 @@ public class Model.DesktopFile : Object {
if (has_key (key)) {
try {
keyfile.remove_key (KeyFileDesktop.GROUP, key);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, err.message);
}
}
}
Expand All @@ -174,8 +174,8 @@ public class Model.DesktopFile : Object {
if (has_key (key)) {
try {
keyfile.remove_key (KeyFileDesktop.GROUP, key);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, err.message);
}
}
}
Expand All @@ -190,8 +190,8 @@ public class Model.DesktopFile : Object {
if (has_key (key)) {
try {
keyfile.remove_key (KeyFileDesktop.GROUP, key);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.remove_key: key=%s: %s", key, err.message);
}
}
}
Expand All @@ -206,8 +206,8 @@ public class Model.DesktopFile : Object {

try {
ret = keyfile.load_from_data (data, data.length, KeyFileFlags.KEEP_TRANSLATIONS);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.load_from_data: %s", e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.load_from_data: %s", err.message);
}

return ret;
Expand Down Expand Up @@ -245,8 +245,8 @@ public class Model.DesktopFile : Object {

try {
val = keyfile.get_locale_string (KeyFileDesktop.GROUP, key, locale);
} catch (KeyFileError e) {
warning ("Failed to KeyFile.get_locale_string: key=%s: %s", key, e.message);
} catch (KeyFileError err) {
warning ("Failed to KeyFile.get_locale_string: key=%s: %s", key, err.message);
}

return val;
Expand All @@ -263,10 +263,10 @@ public class Model.DesktopFile : Object {

try {
ret = keyfile.load_from_file (path, KeyFileFlags.KEEP_TRANSLATIONS);
} catch (FileError e) {
warning ("Failed to load from file. path=%s: %s", path, e.message);
} catch (KeyFileError e) {
warning ("Invalid keyfile. path=%s: %s", path, e.message);
} catch (FileError err) {
warning ("Failed to load from file. path=%s: %s", path, err.message);
} catch (KeyFileError err) {
warning ("Invalid keyfile. path=%s: %s", path, err.message);
}

return ret;
Expand All @@ -277,8 +277,8 @@ public class Model.DesktopFile : Object {

try {
ret = keyfile.save_to_file (path);
} catch (FileError e) {
warning ("Failed to save file. path=%s: %s", path, e.message);
} catch (FileError err) {
warning ("Failed to save file. path=%s: %s", path, err.message);
}

return ret;
Expand All @@ -290,8 +290,8 @@ public class Model.DesktopFile : Object {

try {
ret = file.delete ();
} catch (Error e) {
warning ("Failed to delete file. path=%s: %s", path, e.message);
} catch (Error err) {
warning ("Failed to delete file. path=%s: %s", path, err.message);
}

return ret;
Expand All @@ -314,9 +314,9 @@ public class Model.DesktopFile : Object {
bool ret = false;
try {
ret = yield file_launcher.launch (parent, null);
} catch (Error e) {
warning ("Failed to open file externally. path=%s: %s", path, e.message);
throw e;
} catch (Error err) {
warning ("Failed to open file externally. path=%s: %s", path, err.message);
throw err;
}

return ret;
Expand Down
22 changes: 15 additions & 7 deletions src/Model/DesktopFileModel.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class Model.DesktopFileModel : Object {
if (!FileUtils.test (desktop_files_path, FileTest.EXISTS)) {
try {
desktop_files_dir.make_directory_with_parents ();
} catch (Error e) {
warning (e.message);
} catch (Error err) {
warning ("Failed to make directory. path=%s: %s", desktop_files_path, err.message);
}
}
}
Expand All @@ -66,11 +66,19 @@ public class Model.DesktopFileModel : Object {
new Thread<void> (null, () => {
files_list.clear ();

FileEnumerator enumerator;
try {
var enumerator = desktop_files_dir.enumerate_children (FileAttribute.STANDARD_NAME,
FileQueryInfoFlags.NONE);
FileInfo file_info;
enumerator = desktop_files_dir.enumerate_children (FileAttribute.STANDARD_NAME,
FileQueryInfoFlags.NONE);
} catch (Error err) {
warning ("Failed to get information of files: %s", err.message);
// Schedule to let the UI thread resume from the yield sentence.
Idle.add (load.callback);
return;
}

FileInfo file_info;
try {
// Check and address the files in the desktop_files_path directory one by one
while ((file_info = enumerator.next_file ()) != null) {
// We handle only the desktop file in this app, so ignore any files without the .desktop suffix
Expand All @@ -91,8 +99,8 @@ public class Model.DesktopFileModel : Object {

files_list.add (file);
}
} catch (Error e) {
warning (e.message);
} catch (Error err) {
warning ("Failed to load file info: %s", err.message);
// Schedule to let the UI thread resume from the yield sentence.
Idle.add (load.callback);
return;
Expand Down
16 changes: 8 additions & 8 deletions src/View/EditView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ public class View.EditView : Adw.NavigationPage {
exec_entry.text = path;
desktop_file.set_string (KeyFileDesktop.KEY_EXEC, path);
set_save_button_sensitivity ();
} catch (Error e) {
warning ("Failed to select executable file: %s", e.message);
} catch (Error err) {
warning ("Failed to select executable file: %s", err.message);
}
});
});
Expand All @@ -261,8 +261,8 @@ public class View.EditView : Adw.NavigationPage {

try {
icon_image.gicon = Icon.new_for_string (icon_entry.text);
} catch (Error e) {
warning ("Failed to update icon_image: %s", e.message);
} catch (Error err) {
warning ("Failed to update icon_image: %s", err.message);
}

desktop_file.set_string (KeyFileDesktop.KEY_ICON, icon_entry.text);
Expand Down Expand Up @@ -305,8 +305,8 @@ public class View.EditView : Adw.NavigationPage {

icon_entry.text = path;
desktop_file.set_string (KeyFileDesktop.KEY_ICON, path);
} catch (Error e) {
warning ("Failed to select icon file: %s", e.message);
} catch (Error err) {
warning ("Failed to select icon file: %s", err.message);
}
});
});
Expand Down Expand Up @@ -403,8 +403,8 @@ public class View.EditView : Adw.NavigationPage {

try {
icon_image.gicon = Icon.new_for_string (icon);
} catch (Error e) {
warning ("Failed to update icon_image: %s", e.message);
} catch (Error err) {
warning ("Failed to update icon_image: %s", err.message);
}

string locale = desktop_file.get_locale_for_key (KeyFileDesktop.KEY_NAME, Application.preferred_language);
Expand Down
4 changes: 2 additions & 2 deletions src/View/FilesView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public class View.FilesView : Adw.NavigationPage {
};
try {
app_icon.gicon = Icon.new_for_string (file.get_string (KeyFileDesktop.KEY_ICON, false));
} catch (Error e) {
warning ("Failed to update app_icon: %s", e.message);
} catch (Error err) {
warning ("Failed to update app_icon: %s", err.message);
}

string locale = file.get_locale_for_key (KeyFileDesktop.KEY_NAME, Application.preferred_language);
Expand Down

0 comments on commit 88a25e8

Please sign in to comment.