From a2dca2b76247157376d91d7948adb99e7626f224 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 14 Jan 2023 22:09:43 -0500 Subject: [PATCH] HexEditor: Propagate errors when using "Save" --- Userland/Applications/HexEditor/HexDocument.cpp | 7 ++++--- Userland/Applications/HexEditor/HexDocument.h | 2 +- Userland/Applications/HexEditor/HexEditor.cpp | 11 +++++------ Userland/Applications/HexEditor/HexEditor.h | 2 +- Userland/Applications/HexEditor/HexEditorWidget.cpp | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Userland/Applications/HexEditor/HexDocument.cpp b/Userland/Applications/HexEditor/HexDocument.cpp index 7b27003b9f..2ce1677a6e 100644 --- a/Userland/Applications/HexEditor/HexDocument.cpp +++ b/Userland/Applications/HexEditor/HexDocument.cpp @@ -83,15 +83,16 @@ HexDocumentFile::HexDocumentFile(NonnullOwnPtr file) { } -void HexDocumentFile::write_to_file() +ErrorOr HexDocumentFile::write_to_file() { for (auto& change : m_changes) { - m_file->seek(change.key, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors(); - m_file->write({ &change.value, 1 }).release_value_but_fixme_should_propagate_errors(); + TRY(m_file->seek(change.key, SeekMode::SetPosition)); + TRY(m_file->write({ &change.value, 1 })); } clear_changes(); // make sure the next get operation triggers a read m_buffer_file_pos = m_file_size + 1; + return {}; } ErrorOr HexDocumentFile::write_to_file(Core::Stream::File& file) diff --git a/Userland/Applications/HexEditor/HexDocument.h b/Userland/Applications/HexEditor/HexDocument.h index 00fe630868..92c5f7ca09 100644 --- a/Userland/Applications/HexEditor/HexDocument.h +++ b/Userland/Applications/HexEditor/HexDocument.h @@ -66,7 +66,7 @@ public: void set_file(NonnullOwnPtr file); NonnullOwnPtr const& file() const; - void write_to_file(); + ErrorOr write_to_file(); ErrorOr write_to_file(Core::Stream::File& file); Cell get(size_t position) override; u8 get_unchanged(size_t position) override; diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index f5cb6b6b88..623f33c242 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -153,14 +153,13 @@ ErrorOr HexEditor::save_as(NonnullOwnPtr new_file) return {}; } -bool HexEditor::save() +ErrorOr HexEditor::save() { - if (m_document->type() != HexDocument::Type::File) { - return false; - } + if (m_document->type() != HexDocument::Type::File) + return Error::from_string_literal("Unable to save from a memory document"); - static_cast(m_document.ptr())->write_to_file(); - return true; + TRY(static_cast(m_document.ptr())->write_to_file()); + return {}; } size_t HexEditor::selection_size() diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index 35ff1dfb8c..4a27d486d5 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -39,7 +39,7 @@ public: ErrorOr fill_selection(u8 fill_byte); Optional get_byte(size_t position); ErrorOr save_as(NonnullOwnPtr); - bool save(); + ErrorOr save(); bool undo(); bool redo(); diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index e433d56db1..80a879dfa3 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -132,8 +132,8 @@ HexEditorWidget::HexEditorWidget() if (m_path.is_empty()) return m_save_as_action->activate(); - if (!m_editor->save()) { - GUI::MessageBox::show(window(), "Unable to save file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error); + if (auto result = m_editor->save(); result.is_error()) { + GUI::MessageBox::show(window(), DeprecatedString::formatted("Unable to save file: {}\n"sv, result.error()), "Error"sv, GUI::MessageBox::Type::Error); } else { window()->set_modified(false); m_editor->update();