From af2b6015e2811ef441b279b579d3bcb39aa31927 Mon Sep 17 00:00:00 2001 From: NiJingzhe <1448976235@qq.com> Date: Fri, 30 Oct 2026 02:21:47 +0800 Subject: [PATCH] refactor(catch): every swallowed failure is an explicit, narrowed fallback Behaviour is unchanged: each empty catch body now assigns/returns the value the code already ended up with (nullopt, 0.0 term, skipped selector keys), and the caught type is narrowed where the guarded call is known to throw only that type (Standard_Failure, KernelError, scad::Exception, ValueError). Sites whose type could not be proven (ql loop_role/geomclosed, geo_selection shape_geom_type) keep std::exception and say so. Session::~Session drops a try/catch around the noexcept stop(). Checked by: ninja build, ctest 14/14, src/cpp/tests/python 471 passed/1 skipped, and the whole old test/ suite (no failures). clang-tidy itself could not be run locally. Co-Authored-By: Claude Sonnet 5.5 --- src/cpp/src/kernel/properties.cpp | 81 +++++++++++--------- src/cpp/src/ops/features/blend_diagnosis.cpp | 13 +++- src/cpp/src/product/geo_selector.cpp | 17 +++- src/cpp/src/ql/property.cpp | 28 +++++-- src/cpp/src/reactive/geo_selection.cpp | 28 +++++-- src/cpp/src/reactive/selection.cpp | 9 ++- src/cpp/src/reactive/session.cpp | 11 +-- src/cpp/src/sketch/solver/backend.cpp | 6 +- src/cpp/src/surface/hooks.cpp | 5 +- 9 files changed, 132 insertions(+), 66 deletions(-) diff --git a/src/cpp/src/kernel/properties.cpp b/src/cpp/src/kernel/properties.cpp index bbddf74b..d80689f9 100644 --- a/src/cpp/src/kernel/properties.cpp +++ b/src/cpp/src/kernel/properties.cpp @@ -9,10 +9,12 @@ #include #include #include +#include #include #include #include +#include #include "scad/core/error.hpp" #include "scad/kernel/convert.hpp" @@ -33,6 +35,23 @@ bool mass_point(const GProp_GProps& props, Vec3& out, double eps = 1e-12) { return true; } +// The centre of mass `measure` integrates, or nullopt when OCCT's integrator fails or reports a zero mass. +template +std::optional measured_mass_point(Measure&& measure) { + try { + GProp_GProps props; + measure(props); + Vec3 point; + if (mass_point(props, point)) + return point; + return std::nullopt; + } catch (const Standard_Failure&) { + // The integrator failing is expected on degenerate shapes: the caller falls back to the next + // measure (and finally the bounding-box centre), so nothing is reported here. + return std::nullopt; + } +} + } // namespace double linear_length(const TopoDS_Shape& shape) { @@ -86,54 +105,44 @@ Vec3 center_of_mass(const TopoDS_Shape& shape) { if (shape.IsNull()) throw ValueError("center_of_mass of a null shape"); return occt_guard([&]() -> Vec3 { - Vec3 point; - GProp_GProps props; switch (shape.ShapeType()) { case TopAbs_SOLID: case TopAbs_COMPSOLID: - case TopAbs_COMPOUND: - try { + case TopAbs_COMPOUND: { + const auto volumetric = measured_mass_point([&](GProp_GProps& props) { BRepGProp::VolumePropertiesGK(shape, props, tol::kVolumeIntegration, true, true); - if (mass_point(props, point)) - return point; - } catch (...) { - } + }); + if (volumetric) + return *volumetric; if (shape.ShapeType() == TopAbs_COMPOUND) { // A compound may hold only faces or edges. - try { - GProp_GProps surface; - BRepGProp::SurfaceProperties(shape, surface); - if (mass_point(surface, point)) - return point; - } catch (...) { - } - try { - GProp_GProps linear; - BRepGProp::LinearProperties(shape, linear); - if (mass_point(linear, point)) - return point; - } catch (...) { - } + const auto surface = + measured_mass_point([&](GProp_GProps& props) { BRepGProp::SurfaceProperties(shape, props); }); + if (surface) + return *surface; + const auto linear = + measured_mass_point([&](GProp_GProps& props) { BRepGProp::LinearProperties(shape, props); }); + if (linear) + return *linear; } return bounding_box(shape).center(); + } case TopAbs_FACE: - case TopAbs_SHELL: - try { - BRepGProp::SurfaceProperties(shape, props); - if (mass_point(props, point)) - return point; - } catch (...) { - } + case TopAbs_SHELL: { + const auto surface = + measured_mass_point([&](GProp_GProps& props) { BRepGProp::SurfaceProperties(shape, props); }); + if (surface) + return *surface; return bounding_box(shape).center(); + } case TopAbs_WIRE: - case TopAbs_EDGE: - try { - BRepGProp::LinearProperties(shape, props); - if (mass_point(props, point)) - return point; - } catch (...) { - } + case TopAbs_EDGE: { + const auto linear = + measured_mass_point([&](GProp_GProps& props) { BRepGProp::LinearProperties(shape, props); }); + if (linear) + return *linear; return bounding_box(shape).center(); + } case TopAbs_VERTEX: return from_pnt(BRep_Tool::Pnt(TopoDS::Vertex(shape))); default: diff --git a/src/cpp/src/ops/features/blend_diagnosis.cpp b/src/cpp/src/ops/features/blend_diagnosis.cpp index 5c41199e..6db55152 100644 --- a/src/cpp/src/ops/features/blend_diagnosis.cpp +++ b/src/cpp/src/ops/features/blend_diagnosis.cpp @@ -8,6 +8,7 @@ #include #include "probes.hpp" +#include "scad/core/error.hpp" #include "scad/kernel/properties.hpp" #include "scad/kernel/topology.hpp" #include "scad/ops/features/diagnostics.hpp" @@ -389,15 +390,19 @@ void assert_blend_volume_monotonic(const Solid& source, const Solid& result, std // A blend removes a visible share of the volume: when a quick estimate is clearly on the // legitimate side of both thresholds, the precise volumes cannot change the verdict (they are // only needed for the message when it throws). + bool estimate_settles_it = false; try { const double quick_source = kernel::volume_estimate(source.wrapped()); const double quick_result = kernel::volume_estimate(result.wrapped()); const double margin = 1e-3 * std::abs(quick_source); - if (quick_source > 0.0 && quick_result > margin && quick_result < quick_source - margin) - return; - } catch (const std::exception&) { - // fall through to the precise measurement + estimate_settles_it = quick_source > 0.0 && quick_result > margin && quick_result < quick_source - margin; + } catch (const KernelError&) { + // volume_estimate runs under occt_guard, so a failed estimate is a KernelError. It settles nothing; + // the precise measurement below decides (and reports its own failure). + estimate_settles_it = false; } + if (estimate_settles_it) + return; double source_volume = 0.0, result_volume = 0.0; try { source_volume = kernel::volume(source.wrapped()); diff --git a/src/cpp/src/product/geo_selector.cpp b/src/cpp/src/product/geo_selector.cpp index 0e7af2ae..c1ccc9dd 100644 --- a/src/cpp/src/product/geo_selector.cpp +++ b/src/cpp/src/product/geo_selector.cpp @@ -8,6 +8,9 @@ #include #include +#include +#include + #include "scad/core/error.hpp" #include "scad/product/connector.hpp" #include "scad/product/validation.hpp" @@ -123,13 +126,19 @@ Value make_geo_selector(const Shape& shape) { const Edge edge(shape); selector.set("length", edge.length()); selector.set("center", to_array(shape.center())); + std::optional> end_points; try { const Vec3 start = edge.start_vertex().coordinates(); const Vec3 end = edge.end_vertex().coordinates(); - selector.set("start", to_array(start)); - selector.set("end", to_array(end)); - } catch (const std::exception&) { - // An edge without vertices keeps the selector without its end points. + end_points = std::pair(start, end); + } catch (const Exception&) { + // start_vertex/end_vertex throw ValueError for an edge without vertices: the selector then + // keeps no end points (nothing reports it; the selector is still valid). + end_points = std::nullopt; + } + if (end_points) { + selector.set("start", to_array(end_points->first)); + selector.set("end", to_array(end_points->second)); } break; } diff --git a/src/cpp/src/ql/property.cpp b/src/cpp/src/ql/property.cpp index b356a59b..86803662 100644 --- a/src/cpp/src/ql/property.cpp +++ b/src/cpp/src/ql/property.cpp @@ -166,6 +166,9 @@ std::optional loop_role(const Shape& shape) { } catch (const SemanticCapabilityError&) { throw; } catch (const std::exception&) { + // Kept broad: has_tag's other failures are not enumerable here. A shape whose tags cannot be read has + // no loop role; the property is then simply absent. + return std::nullopt; } return std::nullopt; } @@ -242,7 +245,10 @@ std::optional EvalContext::normal(const Shape& shape) const { if (shape.kind() == Kind::Face) { try { slot.value = kernel::face_normal_at(TopoDS::Face(shape.wrapped())); - } catch (const std::exception&) { + } catch (const Exception&) { + // face_normal_at runs under occt_guard (KernelError/ValueError): a normal the kernel cannot + // compute is a missing property, not an error. + slot.value = std::nullopt; } } } @@ -272,7 +278,10 @@ std::optional EvalContext::measure(const Shape& shape, Measure what) con slot.value = Compound(shape).volume(); break; } - } catch (const std::exception&) { // a measurement the kernel cannot make is missing, not an error + } catch (const Exception&) { + // The kernel measures run under occt_guard (KernelError/ValueError): a measurement the kernel cannot + // make is missing, not an error. + slot.value = std::nullopt; } return slot.value; } @@ -431,6 +440,9 @@ std::optional PropertyPath::lookup(const Shape& shape, const EvalContext& if (kind == Kind::Shell) return Value(kernel::shell_is_closed(shape.wrapped())); } catch (const std::exception&) { + // Kept broad: kernel::is_wire_closed/shell_is_closed are not under occt_guard, so this is not + // known to be narrowable. A closedness the kernel cannot tell is a missing property. + return std::nullopt; } return std::nullopt; } @@ -463,17 +475,23 @@ std::optional center_of(const Shape& shape) { } catch (const std::exception&) { return std::nullopt; } - case Kind::Edge: + case Kind::Edge: { + std::optional mass_centre; try { - return shape.center(); - } catch (const std::exception&) { + mass_centre = shape.center(); + } catch (const Exception&) { + // center_of_mass runs under occt_guard (KernelError/ValueError): fall back to the end points below. + mass_centre = std::nullopt; } + if (mass_centre) + return mass_centre; try { // the centre of mass failed: fall back to the midpoint of the end points const Edge edge(shape); return (edge.start_vertex().coordinates() + edge.end_vertex().coordinates()) / 2.0; } catch (const std::exception&) { return std::nullopt; } + } default: return std::nullopt; } diff --git a/src/cpp/src/reactive/geo_selection.cpp b/src/cpp/src/reactive/geo_selection.cpp index e94aa1e1..a1647061 100644 --- a/src/cpp/src/reactive/geo_selection.cpp +++ b/src/cpp/src/reactive/geo_selection.cpp @@ -10,6 +10,8 @@ #include #include +#include +#include #include "scad/core/error.hpp" #include "scad/kernel/properties.hpp" @@ -128,6 +130,9 @@ std::optional shape_geom_type(const Shape& shape) { if (shape.kind() == Kind::Face) return surface_type(BRepAdaptor_Surface(TopoDS::Face(shape.wrapped())).GetType()); } catch (const std::exception&) { + // Kept broad on purpose: OCCT's Standard_Failure does not derive from std::exception, so this catch is + // not known to be narrowable (see the report). A shape without a readable geometry type has none. + return std::nullopt; } return std::nullopt; } @@ -215,12 +220,19 @@ void add_kind_measurements(Value& selector, const Shape& shape) { const Edge edge = shape.as(); selector.set("length", edge.length()); selector.set("center", vec_list(edge.center())); + std::optional> end_points; try { const Vec3 start = edge.start_vertex().coordinates(); const Vec3 end = edge.end_vertex().coordinates(); - selector.set("start", vec_list(start)); - selector.set("end", vec_list(end)); - } catch (const std::exception&) { + end_points = std::pair(start, end); + } catch (const Exception&) { + // start_vertex/end_vertex throw ValueError for an edge without vertices: the selector then keeps + // no end points (nothing reports it; the selector is still valid). + end_points = std::nullopt; + } + if (end_points) { + selector.set("start", vec_list(end_points->first)); + selector.set("end", vec_list(end_points->second)); } break; } @@ -272,10 +284,16 @@ Value make_geo_exact_selector(const Shape& shape, const Shape* source) { } selector.set("metadata_geo", std::move(geo)); add_brep_region_ref(selector, shape); + std::optional bbox; try { - selector.set("bbox", bbox_payload(shape)); - } catch (const std::exception&) { + bbox = bbox_payload(shape); + } catch (const Exception&) { + // Shape::bounding_box runs under occt_guard (KernelError/ValueError): a shape with no box keeps a + // selector without one (nothing reports it). + bbox = std::nullopt; } + if (bbox) + selector.set("bbox", std::move(*bbox)); if (const auto geom = shape_geom_type(shape)) selector.set("geom_type", *geom); diff --git a/src/cpp/src/reactive/selection.cpp b/src/cpp/src/reactive/selection.cpp index c0cb349e..3724842d 100644 --- a/src/cpp/src/reactive/selection.cpp +++ b/src/cpp/src/reactive/selection.cpp @@ -95,15 +95,20 @@ double add_kind_terms(double score, const Shape& shape, const Value& selector) { if (const Value* length = selector.find("length")) score += std::abs(edge.length() - number_or(length, 0.0)) * 10.0; score += center_term(10.0); + double end_point_term = 0.0; try { const Vec3 start = edge.start_vertex().coordinates(); const Vec3 end = edge.end_vertex().coordinates(); if (has_vec(selector.find("start")) && has_vec(selector.find("end"))) { const Vec3 s = vec_of(selector.find("start")), e = vec_of(selector.find("end")); - score += std::min(start.distance(s) + end.distance(e), start.distance(e) + end.distance(s)); + end_point_term = std::min(start.distance(s) + end.distance(e), start.distance(e) + end.distance(s)); } - } catch (const std::exception&) { + } catch (const Exception&) { + // start_vertex/end_vertex throw ValueError for an edge without vertices, as_double a TypeError for + // a malformed stored point: the end points then add nothing to the score (nothing reports it). + end_point_term = 0.0; } + score += end_point_term; break; } case Kind::Wire: { diff --git a/src/cpp/src/reactive/session.cpp b/src/cpp/src/reactive/session.cpp index 5ac517b4..cdd7a537 100644 --- a/src/cpp/src/reactive/session.cpp +++ b/src/cpp/src/reactive/session.cpp @@ -409,13 +409,10 @@ Session::Session(Options options) registry_(options.registry != nullptr ? options.registry : &ExecutorRegistry::global()) {} Session::~Session() { - if (scope_) { - // Destroyed while still installed: leave the thread as it was found. - try { - stop(); - } catch (...) { - } - } + // Destroyed while still installed: leave the thread as it was found. stop() is noexcept, so nothing + // can escape this destructor and no try/catch is needed around it. + if (scope_) + stop(); } void Session::start() { diff --git a/src/cpp/src/sketch/solver/backend.cpp b/src/cpp/src/sketch/solver/backend.cpp index 690c1caa..57f1fd94 100644 --- a/src/cpp/src/sketch/solver/backend.cpp +++ b/src/cpp/src/sketch/solver/backend.cpp @@ -115,8 +115,10 @@ ScopedSketchSolverBackend::ScopedSketchSolverBackend(std::shared_ptr& sections, std::string } catch (const Error&) { throw; } catch (const std::exception&) { - // A diagnoser that itself fails explains nothing; the caller applies the generic wrap. + // A diagnosis never replaces the error it explains: the caller applies the generic wrap. Kept broad, + // the diagnoser is an arbitrary hook. + return; } catch (...) { // Same for non-std exceptions (OpenCASCADE's). + return; } }