diff --git a/crates/engine/src/cmd/sketch_freeform.rs b/crates/engine/src/cmd/sketch_freeform.rs index d64aa80f9..d9cefed2b 100644 --- a/crates/engine/src/cmd/sketch_freeform.rs +++ b/crates/engine/src/cmd/sketch_freeform.rs @@ -176,7 +176,7 @@ fn add_pieces(sk: &mut Sketch, segs: &[Seg2], start: Option, end: Option< pts.push(to); CurveKind::Spline { pts, control: true, degree: n, periodic: false, nurbs: None } } - Seg2::Arc { .. } => return Err(bad("trim", "unexpected arc piece")), + Seg2::Arc { .. } => return Err(bad("sketch.trim", "unexpected arc piece")), }; let c = sk.add_curve(kind, None)?; if let Some(cu) = sk.curves.get_mut(c) { diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs index 05675089e..ac1b93587 100644 --- a/crates/engine/src/cmd/sketch_modify.rs +++ b/crates/engine/src/cmd/sketch_modify.rs @@ -233,9 +233,12 @@ fn pin(sk: &mut Sketch, p: usize, cutter: usize) -> Result<()> { /// Split curve `ci` at interior parameters `ts` (sorted). Returns the pieces in parameter /// order (the first keeps the original index) and the new points. -fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64]) -> Result<(Vec, Vec)> { - let sh = sk.shape(ci).ok_or_else(|| bad("split", "curve"))?; - let kind = sk.curves.get(ci).map(|c| c.kind.clone()).ok_or_else(|| bad("split", "curve"))?; +/// +/// `cmd` is the command the caller used (`sketch.trim`, `sketch.trim.path`, `sketch.break`): +/// a refusal names the command the caller can write, never an internal step. +fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64], cmd: &str) -> Result<(Vec, Vec)> { + let sh = sk.shape(ci).ok_or_else(|| bad(cmd, "curve"))?; + let kind = sk.curves.get(ci).map(|c| c.kind.clone()).ok_or_else(|| bad(cmd, "curve"))?; let construction = sk.curves.get(ci).is_some_and(|c| c.construction); let mut pts = Vec::new(); for t in ts { @@ -273,7 +276,7 @@ fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64]) -> Result<(Vec, Vec { if pts.len() < 2 { - return Err(bad("split", "a circle needs two points to split at")); + return Err(bad(cmd, "a circle needs two points to split at")); } let mut chain = pts.clone(); if let Some(f) = pts.first() { @@ -287,7 +290,7 @@ fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64]) -> Result<(Vec, Vec return Err(bad("split", "not supported yet for ellipses, splines and conics")), + _ => return Err(bad(cmd, "not supported yet for ellipses, splines and conics")), } Ok((pieces, pts)) } @@ -668,7 +671,7 @@ fn remove_piece(sk: &mut Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<(Valu release_extent(sk, ci); let tangencies = endpoint_tangencies(sk, ci); let ts: Vec = cuts.iter().map(|c| c.0).collect(); - let (pieces, pts) = split_at(sk, ci, &ts)?; + let (pieces, pts) = split_at(sk, ci, &ts, cmd)?; // The piece containing tq. let k = if full { let (a, b) = (ts[0], ts[1]); @@ -938,7 +941,7 @@ fn break_cmd(s: &mut Session, p: &Value) -> Result { // Its length (and its midpoint, an equal length) measured the whole curve: left on the // first piece, it would stretch that piece back to the old size and skew the sketch. release_extent(sk, ci); - let (pieces, pts) = split_at(sk, ci, &ts)?; + let (pieces, pts) = split_at(sk, ci, &ts, cmd)?; for (pi, c) in pts.iter().zip(&cutters) { if let Some(c) = c { pin(sk, *pi, *c)?; diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index dae705393..91797d98b 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -5545,3 +5545,22 @@ fn breaking_again_where_a_curve_was_broken_adds_nothing() { } assert!(k.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Distance { value, .. } if close(value, 20.0))), "{:?}", k.constraints); } + +/// #4712: the split inside the sketch modify tools refuses in the name of the public +/// command the caller used, not an internal step: an error must never name a command the +/// caller cannot write. +#[test] +fn split_failures_name_the_public_command() { + let mut s = new_sketch(); + // A circle, split at one point: the circle needs two points to split at. + let circle = ids(&run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 10}))["curves"])[0].clone(); + let error = s.execute("sketch.break", &json!({"curve": circle, "points": [[10, 0]]})).expect_err("a circle needs two points to split at"); + let crate::EngineError::BadParams { cmd, msg } = &error else { panic!("a parameter error: {error}") }; + assert_eq!(cmd, "sketch.break", "{error}"); + assert_eq!(msg, "a circle needs two points to split at"); + // The message the caller reads names the public command, never an internal step. + let text = error.to_string(); + assert!(text.contains("invalid parameters for `sketch.break`"), "{text}"); + assert!(!text.contains("invalid parameters for `split`"), "{text}"); + assert!(!text.contains("sketch.modify"), "{text}"); +}