Skip to content

Commit

Permalink
[cli][daemon] move the cli suggestion part of error message to cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeliao committed Aug 30, 2024
1 parent 5ed2dfb commit bafd3f9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/client/cli/cmd/stop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ mp::ReturnCode cmd::Stop::run(mp::ArgParser* parser)
AnimatedSpinner spinner{cout};
auto on_failure = [this, &spinner](grpc::Status& status) {
spinner.stop();
return standard_failure_handler_for(name(), cerr, status);

// grpc::StatusCode::INVALID_ARGUMENT matches mp::VMStateInvalidException
return status.error_code() == grpc::StatusCode::INVALID_ARGUMENT
? standard_failure_handler_for(name(), cerr, status, "Use --force to power it off.")
: standard_failure_handler_for(name(), cerr, status);

Check warning on line 51 in src/client/cli/cmd/stop.cpp

View check run for this annotation

Codecov / codecov/patch

src/client/cli/cmd/stop.cpp#L49-L51

Added lines #L49 - L51 were not covered by tests
};

spinner.start(instance_action_message_for(request.instance_names(), "Stopping "));
Expand Down
4 changes: 4 additions & 0 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,10 @@ try // clang-format on

status_promise->set_value(status);
}
catch (const mp::VMStateInvalidException& e)

Check warning on line 2149 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2149

Added line #L2149 was not covered by tests
{
status_promise->set_value(grpc::Status{grpc::StatusCode::INVALID_ARGUMENT, e.what()});

Check warning on line 2151 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2151

Added line #L2151 was not covered by tests
}
catch (const std::exception& e)
{
status_promise->set_value(grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, e.what(), ""));
Expand Down
8 changes: 3 additions & 5 deletions src/platform/backends/shared/base_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ std::string mp::BaseVirtualMachine::get_instance_id_from_the_cloud_init() const

void mp::BaseVirtualMachine::check_state_for_shutdown(bool force)
{
const std::string force_statement{"Use --force to override."};

// A mutex should already be locked by the caller here
if (state == State::off || state == State::stopped)
{
Expand All @@ -205,17 +203,17 @@ void mp::BaseVirtualMachine::check_state_for_shutdown(bool force)

if (state == State::suspending)
{
throw VMStateInvalidException{fmt::format("Cannot stop instance while suspending. {}", force_statement)};
throw VMStateInvalidException{"Cannot stop instance while suspending."};
}

if (state == State::suspended)
{
throw VMStateInvalidException{fmt::format("Cannot stop suspended instance. {}", force_statement)};
throw VMStateInvalidException{"Cannot stop suspended instance."};
}

if (state == State::starting || state == State::restarting)
{
throw VMStateInvalidException{fmt::format("Cannot stop instance while starting. {}", force_statement)};
throw VMStateInvalidException{"Cannot stop instance while starting."};
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/lxd/test_lxd_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ TEST_F(LXDBackend, shutdown_while_stopped_does_nothing_and_logs_debug)

TEST_F(LXDBackend, shutdown_while_frozen_throws_and_logs_info)
{
const std::string error_msg{"Cannot stop suspended instance. Use --force to override."};
const std::string error_msg{"Cannot stop suspended instance."};
mpt::MockVMStatusMonitor mock_monitor;

EXPECT_CALL(*mock_network_access_manager, createRequest(_, _, _)).WillRepeatedly([](auto, auto request, auto) {
Expand Down
6 changes: 3 additions & 3 deletions tests/qemu/test_qemu_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ TEST_F(QemuBackend, machine_unknown_state_properly_shuts_down)

TEST_F(QemuBackend, suspendedStateNoForceShutdownThrows)
{
const std::string error_msg{"Cannot stop suspended instance. Use --force to override."};
const std::string error_msg{"Cannot stop suspended instance."};

EXPECT_CALL(*mock_qemu_platform_factory, make_qemu_platform(_)).WillOnce([this](auto...) {
return std::move(mock_qemu_platform);
Expand All @@ -394,7 +394,7 @@ TEST_F(QemuBackend, suspendedStateNoForceShutdownThrows)

TEST_F(QemuBackend, suspendingStateNoForceShutdownThrows)
{
const std::string error_msg{"Cannot stop instance while suspending. Use --force to override."};
const std::string error_msg{"Cannot stop instance while suspending."};

EXPECT_CALL(*mock_qemu_platform_factory, make_qemu_platform(_)).WillOnce([this](auto...) {
return std::move(mock_qemu_platform);
Expand All @@ -414,7 +414,7 @@ TEST_F(QemuBackend, suspendingStateNoForceShutdownThrows)

TEST_F(QemuBackend, startingStateNoForceShutdownThrows)
{
const std::string error_msg{"Cannot stop instance while starting. Use --force to override."};
const std::string error_msg{"Cannot stop instance while starting."};

EXPECT_CALL(*mock_qemu_platform_factory, make_qemu_platform(_)).WillOnce([this](auto...) {
return std::move(mock_qemu_platform);
Expand Down

0 comments on commit bafd3f9

Please sign in to comment.