summaryrefslogtreecommitdiff
path: root/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp
diff options
context:
space:
mode:
authorRasmusNylander <43042651+rasmusnylander@users.noreply.github.com>2021-12-16 19:39:38 +0100
committerAndreas Kling <kling@serenityos.org>2022-01-06 17:54:03 +0100
commit9678ff15a8cf6ba5caa051aa8f4d5345f95a705c (patch)
tree8213f5d1adbd321b5eb99904157312eb9715bd8a /Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp
parent4e65c4dae4d79d533fa1a3622544a9a0eaeb8c88 (diff)
downloadserenity-9678ff15a8cf6ba5caa051aa8f4d5345f95a705c.zip
KeyboardMapper: Propagate errors using ErrorOr and TRY
KeyboardMapperWidget's load_map_from_file, load_map_from_system, save, and save_to_file now all return ErrorOr<void> and no longer handles alerting the user to potential errors. main is now responsible for handling errors originating from its calls to these four functions; it will simply alert the user using the new method KeyboardMapperWidget::show_error_to_user(Error), which simply creates a MassageBox displaying the error's string_literal. This makes the whole program slight more clean feeling :^).
Diffstat (limited to 'Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp')
-rw-r--r--Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp58
1 files changed, 20 insertions, 38 deletions
diff --git a/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp b/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp
index ef6e6d846e..14187f3e6e 100644
--- a/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp
+++ b/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp
@@ -114,17 +114,12 @@ u32* KeyboardMapperWidget::map_from_name(const StringView map_name)
return map;
}
-void KeyboardMapperWidget::load_from_file(String filename)
+ErrorOr<void> KeyboardMapperWidget::load_map_from_file(const String& filename)
{
- auto result = Keyboard::CharacterMapFile::load_from_file(filename);
- if (!result.has_value()) {
- auto error_message = String::formatted("Failed to load character map from file {}", filename);
- GUI::MessageBox::show(window(), error_message, "Error", GUI::MessageBox::Type::Error);
- return;
- }
+ auto character_map = TRY(Keyboard::CharacterMapFile::load_from_file(filename));
m_filename = filename;
- m_character_map = result.value();
+ m_character_map = character_map;
set_current_map("map");
for (auto& widget : m_map_group->child_widgets()) {
@@ -133,15 +128,15 @@ void KeyboardMapperWidget::load_from_file(String filename)
}
update_window_title();
+ return {};
}
-void KeyboardMapperWidget::load_from_system()
+ErrorOr<void> KeyboardMapperWidget::load_map_from_system()
{
- auto result = Keyboard::CharacterMap::fetch_system_map();
- VERIFY(!result.is_error());
+ auto character_map = TRY(Keyboard::CharacterMap::fetch_system_map());
- m_filename = String::formatted("/res/keymaps/{}.json", result.value().character_map_name());
- m_character_map = result.value().character_map_data();
+ m_filename = String::formatted("/res/keymaps/{}.json", character_map.character_map_name());
+ m_character_map = character_map.character_map_data();
set_current_map("map");
for (auto& widget : m_map_group->child_widgets()) {
@@ -150,14 +145,15 @@ void KeyboardMapperWidget::load_from_system()
}
update_window_title();
+ return {};
}
-void KeyboardMapperWidget::save()
+ErrorOr<void> KeyboardMapperWidget::save()
{
- save_to_file(m_filename);
+ return save_to_file(m_filename);
}
-void KeyboardMapperWidget::save_to_file(StringView filename)
+ErrorOr<void> KeyboardMapperWidget::save_to_file(StringView filename)
{
JsonObject map_json;
@@ -182,34 +178,16 @@ void KeyboardMapperWidget::save_to_file(StringView filename)
// Write to file.
String file_content = map_json.to_string();
-
- auto file = Core::File::construct(filename);
- file->open(Core::OpenMode::WriteOnly);
- if (!file->is_open()) {
- StringBuilder sb;
- sb.append("Failed to open ");
- sb.append(filename);
- sb.append(" for write. Error: ");
- sb.append(file->error_string());
-
- GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
- return;
- }
+ auto file = TRY(Core::File::open(filename, Core::OpenMode::WriteOnly));
bool result = file->write(file_content);
- if (!result) {
- int error_number = errno;
- StringBuilder sb;
- sb.append("Unable to save file. Error: ");
- sb.append(strerror(error_number));
-
- GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
- return;
- }
+ if (!result)
+ return Error::from_errno(file->error());
m_modified = false;
m_filename = filename;
update_window_title();
+ return {};
}
void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
@@ -265,3 +243,7 @@ void KeyboardMapperWidget::update_window_title()
window()->set_title(sb.to_string());
}
+
+void KeyboardMapperWidget::show_error_to_user(Error error){
+ GUI::MessageBox::show_error(window(), error.string_literal());
+} \ No newline at end of file