diff --git a/Userland/Applications/TextEditor/MainWidget.cpp b/Userland/Applications/TextEditor/MainWidget.cpp index f67e4a61a9..c8618c83dc 100644 --- a/Userland/Applications/TextEditor/MainWidget.cpp +++ b/Userland/Applications/TextEditor/MainWidget.cpp @@ -276,7 +276,8 @@ MainWidget::MainWidget() if (response.is_error()) return; - read_file(response.value().filename(), response.value().stream()); + if (auto result = read_file(response.value().filename(), response.value().stream()); result.is_error()) + GUI::MessageBox::show(window(), "Unable to open file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error); }); m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { @@ -746,15 +747,12 @@ void MainWidget::update_title() window()->set_title(builder.to_deprecated_string()); } -bool MainWidget::read_file(String const& filename, Core::Stream::File& file) +ErrorOr MainWidget::read_file(String const& filename, Core::Stream::File& file) { - auto result = file.read_until_eof(); - if (result.is_error()) - return false; - m_editor->set_text(result.value()); + m_editor->set_text(TRY(file.read_until_eof())); set_path(filename); m_editor->set_focus(true); - return true; + return {}; } void MainWidget::open_nonexistent_file(DeprecatedString const& path) @@ -809,7 +807,8 @@ void MainWidget::drop_event(GUI::DropEvent& event) auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), urls.first().path()); if (response.is_error()) return; - read_file(response.value().filename(), response.value().stream()); + if (auto result = read_file(response.value().filename(), response.value().stream()); result.is_error()) + GUI::MessageBox::show(window(), "Unable to open file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error); } } diff --git a/Userland/Applications/TextEditor/MainWidget.h b/Userland/Applications/TextEditor/MainWidget.h index c21638e9bd..04ff814e22 100644 --- a/Userland/Applications/TextEditor/MainWidget.h +++ b/Userland/Applications/TextEditor/MainWidget.h @@ -25,7 +25,7 @@ class MainWidget final : public GUI::Widget { public: virtual ~MainWidget() override = default; - bool read_file(String const& filename, Core::Stream::File&); + ErrorOr read_file(String const& filename, Core::Stream::File&); void open_nonexistent_file(DeprecatedString const& path); bool request_close(); diff --git a/Userland/Applications/TextEditor/main.cpp b/Userland/Applications/TextEditor/main.cpp index b7368f3ff5..68920da657 100644 --- a/Userland/Applications/TextEditor/main.cpp +++ b/Userland/Applications/TextEditor/main.cpp @@ -81,8 +81,7 @@ ErrorOr serenity_main(Main::Arguments arguments) else return 1; } else { - if (!text_widget->read_file(response.value().filename(), response.value().stream())) - return 1; + TRY(text_widget->read_file(response.value().filename(), response.value().stream())); text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0)); }