mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:12:45 +00:00 
			
		
		
		
	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 :^).
This commit is contained in:
		
							parent
							
								
									4e65c4dae4
								
							
						
					
					
						commit
						9678ff15a8
					
				
					 3 changed files with 41 additions and 53 deletions
				
			
		|  | @ -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()); | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 RasmusNylander
						RasmusNylander