TextEditor: Propagate errors from read_file()

This commit is contained in:
Lucas CHOLLET 2023-01-14 18:02:35 -05:00 committed by Jelle Raaijmakers
parent 3454185d08
commit a053807be5
Notes: sideshowbarker 2024-07-17 05:05:51 +09:00
3 changed files with 9 additions and 11 deletions

View file

@ -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<void> 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);
}
}

View file

@ -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<void> read_file(String const& filename, Core::Stream::File&);
void open_nonexistent_file(DeprecatedString const& path);
bool request_close();

View file

@ -81,8 +81,7 @@ ErrorOr<int> 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));
}