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 <noreply@anthropic.com>
This commit is contained in:
NiJingzhe
2026-10-30 02:21:47 +08:00
parent 424a8121b4
commit af2b6015e2
9 changed files with 132 additions and 66 deletions
+45 -36
View File
@@ -9,10 +9,12 @@
#include <BRep_Tool.hxx>
#include <Bnd_Box.hxx>
#include <GProp_GProps.hxx>
#include <Standard_Failure.hxx>
#include <TopAbs_Orientation.hxx>
#include <TopoDS.hxx>
#include <cmath>
#include <optional>
#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 <class Measure>
std::optional<Vec3> 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:
+9 -4
View File
@@ -8,6 +8,7 @@
#include <cstdio>
#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());
+13 -4
View File
@@ -8,6 +8,9 @@
#include <GeomAbs_SurfaceType.hxx>
#include <TopoDS.hxx>
#include <optional>
#include <utility>
#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<std::pair<Vec3, Vec3>> 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<Vec3, Vec3>(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;
}
+23 -5
View File
@@ -166,6 +166,9 @@ std::optional<Value> 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<Vec3> 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<double> 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<Value> 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<Vec3> center_of(const Shape& shape) {
} catch (const std::exception&) {
return std::nullopt;
}
case Kind::Edge:
case Kind::Edge: {
std::optional<Vec3> 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;
}
+23 -5
View File
@@ -10,6 +10,8 @@
#include <TopTools_IndexedMapOfShape.hxx>
#include <map>
#include <optional>
#include <utility>
#include "scad/core/error.hpp"
#include "scad/kernel/properties.hpp"
@@ -128,6 +130,9 @@ std::optional<std::string> 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<Edge>();
selector.set("length", edge.length());
selector.set("center", vec_list(edge.center()));
std::optional<std::pair<Vec3, Vec3>> 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<Vec3, Vec3>(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<Value> 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);
+7 -2
View File
@@ -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: {
+4 -7
View File
@@ -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() {
+4 -2
View File
@@ -115,8 +115,10 @@ ScopedSketchSolverBackend::ScopedSketchSolverBackend(std::shared_ptr<SketchSolve
ScopedSketchSolverBackend::~ScopedSketchSolverBackend() {
try {
set_default_sketch_solver_backend(previous_);
} catch (const std::exception&) {
// The previous default was registered when we captured it; nothing to do if it vanished.
} catch (const ValueError&) {
// get_locked throws ValueError for a name that is no longer registered. The previous default was
// registered when we captured it, and a destructor must not throw: the default stays as it is.
return;
}
}
+4 -1
View File
@@ -35,9 +35,12 @@ void diagnose_loft_failure(const std::vector<LoftSection>& 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;
}
}