错误处理

This commit is contained in:
2026-08-16 20:39:12 +08:00
parent b33b57b0e3
commit 9d544c7ba9
44 changed files with 469 additions and 459 deletions
+2 -2
View File
@@ -207,7 +207,7 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
[[nodiscard]] std::unique_lock<std::mutex> acquire_foreground_lock() {
return std::unique_lock<std::mutex>(mutex);
}
void disarm_automatic_render() noexcept {
void disarm_automatic_render() {
++automatic_timer_revision;
if (automatic_timer == trantor::InvalidTimerId)
return;
@@ -264,7 +264,7 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
std::lock_guard lock(mutex);
if (generation == scene_generation && scene && scene->can_render_automatically()) {
const auto error = scene->render_latest_frame();
if (error == Gallery_Render_Error::none)
if (error == Gallery_Render_Result::none)
last_render_started = started;
}
render_task_pending = false;
@@ -2,6 +2,7 @@
#include "Gallery_Plot_Session.h"
#include "Web_Event_Adapter.h"
#include <renderive/error/Error_Policy.hpp>
#include <trantor/utils/Logger.h>
@@ -58,8 +59,10 @@ void Gallery_WebSocket_Controller::handleNewMessage(
connection->send(response->payload.data(), response->payload.size(),
message_type);
}
} catch (const std::exception& error) {
LOG_ERROR << "Renderive Gallery session failed: " << error.what();
} catch (...) {
static_cast<void>(::renderive::error::capture(
"handling Gallery WebSocket request", std::current_exception()));
LOG_ERROR << "Renderive Gallery session failed";
connection->shutdown(drogon::CloseCode::kUnexpectedCondition,
"Renderive Gallery rendering failed");
}
@@ -2,6 +2,7 @@
#include "Web_Event_Adapter.h"
#include "Web_Plot_Session.h"
#include <renderive/error/Error_Policy.hpp>
#include <trantor/utils/Logger.h>
@@ -57,8 +58,10 @@ void Renderive_WebSocket_Controller::handleNewMessage(
connection->send(response->payload.data(), response->payload.size(),
message_type);
}
} catch (const std::exception& error) {
LOG_ERROR << "Renderive WebSocket session failed: " << error.what();
} catch (...) {
static_cast<void>(::renderive::error::capture(
"handling Renderive WebSocket request", std::current_exception()));
LOG_ERROR << "Renderive WebSocket session failed";
connection->shutdown(drogon::CloseCode::kUnexpectedCondition,
"Renderive rendering failed");
}
+2 -2
View File
@@ -160,7 +160,7 @@ struct Web_Plot_Session::Impl {
plot.activate_view();
update_model();
const auto initial_render = plot.render_frame(true);
if (initial_render != Plot_Render_Error::none)
if (initial_render != Plot_Render_Result::none)
plot.request_redraw();
}
~Impl() {
@@ -268,7 +268,7 @@ struct Web_Plot_Session::Impl {
if (!plot.view_active())
return std::nullopt;
update_model();
if (plot.render_frame(true) != Plot_Render_Error::none)
if (plot.render_frame(true) != Plot_Render_Result::none)
return std::nullopt;
std::string pixels;
const Color background = plot.background_color();
@@ -11,7 +11,7 @@
#include <string>
#include <string_view>
namespace renderive::web {
enum class Gallery_Render_Error : std::uint8_t {
enum class Gallery_Render_Result : std::uint8_t {
none,
inactive,
frame_unavailable,
@@ -33,7 +33,7 @@ public:
[[nodiscard]] virtual adminive::Update_Result apply_patch(std::string_view target, const nlohmann::json& patch) = 0;
virtual void resize(int width, int height) = 0;
virtual void dispatch(const Gallery_Input_Event& event) = 0;
[[nodiscard]] virtual Gallery_Render_Error render_latest_frame() = 0;
[[nodiscard]] virtual Gallery_Render_Result render_latest_frame() = 0;
[[nodiscard]] virtual std::optional<std::string> encode_latest_pixels() = 0;
virtual void record_pixel_response(std::chrono::steady_clock::time_point request_started, std::chrono::steady_clock::time_point encode_started, std::chrono::steady_clock::time_point encode_finished, std::size_t pixel_bytes) = 0;
[[nodiscard]] virtual std::string action(const Gallery_Action_Request& request, bool& recognized) = 0;
+24 -24
View File
@@ -29,22 +29,22 @@
#include <vector>
namespace renderive::web {
namespace {
Gallery_Render_Error gallery_render_error(Plot_Render_Error error) {
Gallery_Render_Result gallery_render_error(Plot_Render_Result error) {
switch (error) {
case Plot_Render_Error::none:
return Gallery_Render_Error::none;
case Plot_Render_Error::view_inactive:
return Gallery_Render_Error::inactive;
case Plot_Render_Error::cancelled:
return Gallery_Render_Error::cancelled;
case Plot_Render_Error::deadline_exceeded:
return Gallery_Render_Error::deadline_exceeded;
case Plot_Render_Error::external_failure:
return Gallery_Render_Error::backend_failure;
case Plot_Render_Error::scene_shutting_down:
return Gallery_Render_Error::shutting_down;
case Plot_Render_Result::none:
return Gallery_Render_Result::none;
case Plot_Render_Result::view_inactive:
return Gallery_Render_Result::inactive;
case Plot_Render_Result::cancelled:
return Gallery_Render_Result::cancelled;
case Plot_Render_Result::deadline_exceeded:
return Gallery_Render_Result::deadline_exceeded;
case Plot_Render_Result::external_failure:
return Gallery_Render_Result::backend_failure;
case Plot_Render_Result::scene_shutting_down:
return Gallery_Render_Result::shutting_down;
default:
return Gallery_Render_Error::frame_unavailable;
return Gallery_Render_Result::frame_unavailable;
}
}
template <class Update>
@@ -213,7 +213,7 @@ public:
static_cast<void>(plot_.set_max_render_fps(30.0));
plot_.activate_view();
update_model();
if (plot_.render_frame(true) != Plot_Render_Error::none)
if (plot_.render_frame(true) != Plot_Render_Result::none)
plot_.request_redraw();
}
~Gallery_Scene() {
@@ -349,13 +349,13 @@ public:
}
}, event);
}
Gallery_Render_Error render_latest_frame() override {
Gallery_Render_Result render_latest_frame() override {
if (!plot_.view_active())
return Gallery_Render_Error::inactive;
return Gallery_Render_Result::inactive;
update_model();
const auto started = std::chrono::steady_clock::now();
const auto error = plot_.render_frame(true);
const bool rendered = error == Plot_Render_Error::none;
const bool rendered = error == Plot_Render_Result::none;
record_performance(started, rendered);
if (rendered)
rendered_since_last_pixel_ = true;
@@ -365,7 +365,7 @@ public:
if (!plot_.view_active())
return std::nullopt;
if (!rendered_since_last_pixel_ && !can_render_automatically() &&
render_latest_frame() != Gallery_Render_Error::none)
render_latest_frame() != Gallery_Render_Result::none)
return std::nullopt;
rendered_since_last_pixel_ = false;
std::string pixels;
@@ -420,7 +420,7 @@ public:
}
if (request.id == "mode_prepare" || request.id == "mode_enqueue") {
update_model();
const bool prepared = plot_.prepare_frame() == Plot_Render_Error::none;
const bool prepared = plot_.prepare_frame() == Plot_Render_Result::none;
last_action_result_ = prepared ? "frame prepared" : "prepare rejected";
return prepared ? "Kernel 帧已准备/入队" : "Kernel 拒绝准备帧";
}
@@ -432,19 +432,19 @@ public:
int prepared{};
for (int index = 0; index < count; ++index) {
update_model();
prepared += plot_.prepare_frame() == Plot_Render_Error::none ? 1 : 0;
prepared += plot_.prepare_frame() == Plot_Render_Result::none ? 1 : 0;
}
last_action_result_ = "enqueued=" + std::to_string(prepared);
return "回放帧已批量压入 Flow 队列";
}
if (request.id == "mode_refresh") {
const bool refreshed = plot_.refresh_manual_frame() == Plot_Render_Error::none;
const bool refreshed = plot_.refresh_manual_frame() == Plot_Render_Result::none;
last_action_result_ = refreshed ? "manual refresh succeeded" : "manual refresh failed";
return refreshed ? "Manual 待处理帧已提交刷新" : "当前没有可刷新的 Manual 帧";
}
if (request.id == "mode_render" || request.id == "mode_dequeue") {
const auto started = std::chrono::steady_clock::now();
const bool rendered = plot_.render_prepared_frame() == Plot_Render_Error::none;
const bool rendered = plot_.render_prepared_frame() == Plot_Render_Result::none;
record_performance(started, rendered);
rendered_since_last_pixel_ = rendered;
last_action_result_ = rendered ? "prepared frame rendered" : "no prepared frame";
@@ -458,7 +458,7 @@ public:
if (request.id == "mode_cycle") {
update_model();
const auto started = std::chrono::steady_clock::now();
const bool rendered = plot_.render_frame(true) == Plot_Render_Error::none;
const bool rendered = plot_.render_frame(true) == Plot_Render_Result::none;
record_performance(started, rendered);
rendered_since_last_pixel_ = rendered;
last_action_result_ = rendered ? "full frame cycle rendered" : "frame cycle skipped";
+50 -50
View File
@@ -65,49 +65,49 @@ struct Type_Descriptor<renderive::render_3d::Renderable_Observation> {
namespace renderive::web {
namespace {
using namespace renderive::render_3d;
Gallery_Render_Error gallery_render_error(Frame_Request_Error error) {
Gallery_Render_Result gallery_render_error(Frame_Request_Result error) {
switch (error) {
case Frame_Request_Error::none:
return Gallery_Render_Error::none;
case Frame_Request_Error::cancelled:
return Gallery_Render_Error::cancelled;
case Frame_Request_Error::deadline_exceeded:
return Gallery_Render_Error::deadline_exceeded;
case Frame_Request_Error::external_failure:
return Gallery_Render_Error::backend_failure;
case Frame_Request_Error::scene_shutting_down:
return Gallery_Render_Error::shutting_down;
case Frame_Request_Result::none:
return Gallery_Render_Result::none;
case Frame_Request_Result::cancelled:
return Gallery_Render_Result::cancelled;
case Frame_Request_Result::deadline_exceeded:
return Gallery_Render_Result::deadline_exceeded;
case Frame_Request_Result::external_failure:
return Gallery_Render_Result::backend_failure;
case Frame_Request_Result::scene_shutting_down:
return Gallery_Render_Result::shutting_down;
default:
return Gallery_Render_Error::frame_unavailable;
return Gallery_Render_Result::frame_unavailable;
}
}
Gallery_Render_Error gallery_render_error(Scene_Render_Error error) {
Gallery_Render_Result gallery_render_error(Scene_Render_Result error) {
switch (error) {
case Scene_Render_Error::none:
return Gallery_Render_Error::none;
case Scene_Render_Error::cancelled:
return Gallery_Render_Error::cancelled;
case Scene_Render_Error::deadline_exceeded:
return Gallery_Render_Error::deadline_exceeded;
case Scene_Render_Error::external_failure:
return Gallery_Render_Error::backend_failure;
case Scene_Render_Error::shutting_down:
return Gallery_Render_Error::shutting_down;
case Scene_Render_Result::none:
return Gallery_Render_Result::none;
case Scene_Render_Result::cancelled:
return Gallery_Render_Result::cancelled;
case Scene_Render_Result::deadline_exceeded:
return Gallery_Render_Result::deadline_exceeded;
case Scene_Render_Result::external_failure:
return Gallery_Render_Result::backend_failure;
case Scene_Render_Result::shutting_down:
return Gallery_Render_Result::shutting_down;
}
::renderive::error::unexpected<std::logic_error>("unknown scene render error");
}
Frame_Request_Error frame_request_error(Scene_Render_Error error) {
Frame_Request_Result frame_request_error(Scene_Render_Result error) {
switch (error) {
case Scene_Render_Error::none:
return Frame_Request_Error::none;
case Scene_Render_Error::cancelled:
return Frame_Request_Error::cancelled;
case Scene_Render_Error::deadline_exceeded:
return Frame_Request_Error::deadline_exceeded;
case Scene_Render_Error::external_failure:
return Frame_Request_Error::external_failure;
case Scene_Render_Error::shutting_down:
return Frame_Request_Error::scene_shutting_down;
case Scene_Render_Result::none:
return Frame_Request_Result::none;
case Scene_Render_Result::cancelled:
return Frame_Request_Result::cancelled;
case Scene_Render_Result::deadline_exceeded:
return Frame_Request_Result::deadline_exceeded;
case Scene_Render_Result::external_failure:
return Frame_Request_Result::external_failure;
case Scene_Render_Result::shutting_down:
return Frame_Request_Result::scene_shutting_down;
}
::renderive::error::unexpected<std::logic_error>("unknown scene render error");
}
@@ -359,9 +359,9 @@ public:
last_event_ = "key_rejected";
}
Gallery_Render_Error render_latest_frame() override {
Gallery_Render_Result render_latest_frame() override {
if (!can_render_automatically())
return Gallery_Render_Error::inactive;
return Gallery_Render_Result::inactive;
advance_blue_point(0.025F);
const auto request_error = render_request();
return gallery_render_error(request_error);
@@ -435,17 +435,17 @@ public:
blue_offset_ = std::min(0.25F, blue_offset_ + 0.0125F);
publish_points();
}
const bool prepared = scene_.prepare_frame() == Frame_Request_Error::none;
const bool prepared = scene_.prepare_frame() == Frame_Request_Result::none;
last_event_ = prepared ? "frame_prepared" : "prepare_rejected";
return prepared ? "3D frame prepared" : "3D frame prepare rejected";
}
if (request.id == "mode_refresh") {
const bool refreshed = scene_.refresh_manual_frame() == Frame_Request_Error::none;
const bool refreshed = scene_.refresh_manual_frame() == Frame_Request_Result::none;
last_event_ = refreshed ? "manual_refreshed" : "manual_refresh_failed";
return refreshed ? "Manual 3D frame refreshed" : "No manual frame to refresh";
}
if (request.id == "mode_render" || request.id == "mode_dequeue") {
const bool rendered = render_prepared() == Frame_Request_Error::none;
const bool rendered = render_prepared() == Frame_Request_Result::none;
last_event_ = rendered ? "frame_rendered" : "render_rejected";
return rendered ? "3D frame rendered" : "No 3D frame to render";
}
@@ -629,27 +629,27 @@ private:
}
void render_initial_frame() {
Frame_Request_Error error{Frame_Request_Error::none};
Frame_Request_Result error{Frame_Request_Result::none};
if (frame_mode_ == Gallery_Frame_Mode::Manual) {
if (scene_.prepare_frame() == Frame_Request_Error::none &&
scene_.refresh_manual_frame() == Frame_Request_Error::none)
if (scene_.prepare_frame() == Frame_Request_Result::none &&
scene_.refresh_manual_frame() == Frame_Request_Result::none)
error = render_prepared();
else
error = Frame_Request_Error::no_pending_frame;
error = Frame_Request_Result::no_pending_frame;
} else {
error = render_request();
}
if (error != Frame_Request_Error::none)
if (error != Frame_Request_Result::none)
last_action_result_ = "initial frame is not available";
}
Frame_Request_Error render_request() {
Frame_Request_Result render_request() {
const auto started = std::chrono::steady_clock::now();
++render_attempt_count_;
auto error = scene_.request_frame();
if (error == Frame_Request_Error::none)
if (error == Frame_Request_Result::none)
error = frame_request_error(scene_.render_scene().wait_for_render());
const bool rendered = error == Frame_Request_Error::none;
const bool rendered = error == Frame_Request_Result::none;
last_render_ms_ = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - started)
.count();
@@ -659,13 +659,13 @@ private:
return error;
}
Frame_Request_Error render_prepared() {
Frame_Request_Result render_prepared() {
const auto started = std::chrono::steady_clock::now();
++render_attempt_count_;
auto error = scene_.render_prepared_frame();
if (error == Frame_Request_Error::none)
if (error == Frame_Request_Result::none)
error = frame_request_error(scene_.render_scene().wait_for_render());
const bool rendered = error == Frame_Request_Error::none;
const bool rendered = error == Frame_Request_Result::none;
last_render_ms_ = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - started)
.count();