From ccfd16c51e49f66c83555575db2291d8072bef51 Mon Sep 17 00:00:00 2001 From: SoftFever Date: Sun, 15 Nov 2026 03:26:46 +0800 Subject: [PATCH] Leave a Speed Dial command's unexpected exception to the app's handler --- docs/HLSD/mcp-server.md | 5 ++++- src/slic3r/GUI/NativeCommands.cpp | 16 +++++++++++++--- src/slic3r/GUI/NativeCommands.hpp | 8 ++++++-- tests/slic3rutils/test_command_catalog.cpp | 20 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/docs/HLSD/mcp-server.md b/docs/HLSD/mcp-server.md index aacf0545ad..69c01a138a 100644 --- a/docs/HLSD/mcp-server.md +++ b/docs/HLSD/mcp-server.md @@ -1793,7 +1793,10 @@ Two helpers wrap the function a command's author writes and place it on the thre The palette and the menus call `NativeCommands::run(key, args)`: it checks the arguments, runs the command with a person's context and returns the notice for `notify_result()`, or an Error holding the message when -the arguments are refused, the command throws or it fails without a notice. +the arguments are refused, the command refuses the call (`ToolFailure`) or it fails without a notice. Any +other exception is the command's defect, not an answer to show a person: `run()` logs it with the command's +key and lets it through to the application's handler (`GUI_App::OnExceptionInMainLoop()`), which treats it as +one from any other event handler, so running out of memory still ends the app there. A command whose silent form needs a value has a person's form too, which asks for it: with no arguments (`Mcp::person_form()`), a command opens its dialog as a person's pick does; with arguments that stand for diff --git a/src/slic3r/GUI/NativeCommands.cpp b/src/slic3r/GUI/NativeCommands.cpp index 91f923e498..ef841f31e3 100644 --- a/src/slic3r/GUI/NativeCommands.cpp +++ b/src/slic3r/GUI/NativeCommands.cpp @@ -62,6 +62,7 @@ #include #include #include +#include #include #include #include @@ -1951,20 +1952,29 @@ AppActionRunResult NativeCommands::run(const std::string& key, const json& args) const NativeCommand* command = find(key); if (command == nullptr) return {AppActionRunResult::Level::Info, _L("Unknown command.")}; + return run(*command, args); +} + +AppActionRunResult NativeCommands::run(const NativeCommand& command, const json& args) +{ const auto failed = [](const std::string& why) { return AppActionRunResult{AppActionRunResult::Level::Error, from_u8(why), false}; }; json checked = args; - if (const std::string err = Mcp::argument_error(*command, checked); !err.empty()) + if (const std::string err = Mcp::argument_error(command, checked); !err.empty()) return failed(err); Mcp::PersonContext person; try { - const ToolResult reply = command->run(checked, person); + const ToolResult reply = command.run(checked, person); if (reply.notice) return *reply.notice; if (reply.is_error) return failed(reply.message); return {AppActionRunResult::Level::Success}; + } catch (const ToolFailure& failure) { + return failed(failure.what()); } catch (const std::exception& e) { - return failed(e.what()); + // The log names the command, which the application's handler does not know. + BOOST_LOG_TRIVIAL(error) << "The command " << command.key << " threw: " << e.what(); + throw; } } diff --git a/src/slic3r/GUI/NativeCommands.hpp b/src/slic3r/GUI/NativeCommands.hpp index 33cf8399f8..f74c163e91 100644 --- a/src/slic3r/GUI/NativeCommands.hpp +++ b/src/slic3r/GUI/NativeCommands.hpp @@ -88,9 +88,13 @@ ToolResult from_run_result(const std::string& key, const AppActionRunResult& res // Runs command `key` as a person's pick: checks `args` (Mcp::argument_error()), then runs the command with a // Mcp::PersonContext. Returns what the pick shows, the reply's notice, or an Error holding the message when -// the arguments were refused, the command threw, or it failed with no notice. Unknown keys return a quiet -// Info. UI thread only. +// the arguments were refused, the command refused the call (ToolFailure), or it failed with no notice. Unknown +// keys return a quiet Info, and so does any key while OrcaSlicer is closing. Any other exception is the +// command's defect, not an answer to show: it is logged with the command's key and leaves this function, for +// the application's handler (GUI_App::OnExceptionInMainLoop()) as from any other event handler. UI thread only. AppActionRunResult run(const std::string& key, const nlohmann::json& args = nlohmann::json::object()); +// The same for `command`, which need not be in the process catalog, whether or not OrcaSlicer is closing. +AppActionRunResult run(const NativeCommand& command, const nlohmann::json& args = nlohmann::json::object()); // Shows what a command run from the palette or a menu said: its message as a notification at its level, // nothing for an empty message or a Busy one. UI thread only. diff --git a/tests/slic3rutils/test_command_catalog.cpp b/tests/slic3rutils/test_command_catalog.cpp index 5a70b74c3d..a00891bf44 100644 --- a/tests/slic3rutils/test_command_catalog.cpp +++ b/tests/slic3rutils/test_command_catalog.cpp @@ -494,6 +494,26 @@ TEST_CASE("a person's pick runs the function in place", "[CommandCatalog]") CHECK(result.notice->message == "Done"); } +TEST_CASE("a person's pick shows a command's refusal as an error", "[CommandCatalog]") +{ + const NativeCommand refuses = NativeCommands::ui_command("k", "K", "Commands", no_arguments(), [](const json&) -> AppActionRunResult { + throw ToolFailure("Nothing is selected"); + }); + const AppActionRunResult shown = NativeCommands::run(refuses); + CHECK(shown.level == AppActionRunResult::Level::Error); + CHECK(shown.message == "Nothing is selected"); + CHECK_FALSE(shown.done); +} + +TEST_CASE("an exception that is not a command's refusal leaves a person's pick", "[CommandCatalog]") +{ + // A defect, which the application's handler deals with as it does one from any other event handler. + const NativeCommand breaks = NativeCommands::ui_command("k", "K", "Commands", no_arguments(), [](const json&) -> AppActionRunResult { + throw std::out_of_range("no such element"); + }); + CHECK_THROWS_AS(NativeCommands::run(breaks), std::out_of_range); +} + TEST_CASE("a long command picked by a person is submitted", "[CommandCatalog]") { int calls = 0;