Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

Errors are one line; KLAD_BACKTRACE=1 gives the details

Indicode BVdfb26a98051e
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 9f2e053..e775a42 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -4,7 +4,7 @@ Thank you for looking. klad is young and changes fast; issues with a drawing tha
## Before you start
-- Bugs: open an issue with what you did, what you saw, `klad --version`, your OS, and if you can the `.draw` folder (zipped) or the commands that make it.
+- Bugs: open an issue with what you did, what you saw (run the command again with `KLAD_BACKTRACE=1` for the details of an error), `klad --version`, your OS, and if you can the `.draw` folder (zipped) or the commands that make it.
- Larger changes: open an issue first, so we can agree on the shape before you build it.
- Security problems: see `SECURITY.md`, not the issue tracker.
diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs
index bb9fc9d..9353386 100644
--- a/crates/cli/src/main.rs
+++ b/crates/cli/src/main.rs
@@ -1462,17 +1462,42 @@ fn parse_points(text: &str) -> Result<Vec<[f32; 3]>> {
const CHANGED_EXIT: i32 = 75;
fn main() {
+ let traced = std::env::var_os("KLAD_BACKTRACE").is_some_and(|v| v != "0");
#[cfg(unix)]
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}
+ match traced {
+ true => unsafe { std::env::set_var("RUST_BACKTRACE", "1") },
+ false => std::panic::set_hook(Box::new(|info| eprintln!("error: {}", bug(info)))),
+ }
if let Err(e) = run() {
- eprintln!("Error: {e:?}");
+ match traced {
+ true => eprintln!("error: {e:?}"),
+ false => eprintln!("error: {e:#}"),
+ }
let changed = e.downcast_ref::<klad_core::bundle::Changed>().is_some();
std::process::exit(if changed { CHANGED_EXIT } else { 1 });
}
}
+fn bug(info: &std::panic::PanicHookInfo) -> String {
+ let what = info
+ .payload()
+ .downcast_ref::<&str>()
+ .map(|s| s.to_string())
+ .or_else(|| info.payload().downcast_ref::<String>().cloned())
+ .unwrap_or_default();
+ let place = info
+ .location()
+ .map(|l| format!(" at {}:{}", l.file(), l.line()))
+ .unwrap_or_default();
+ format!(
+ "klad hit a bug: {}{place}. KLAD_BACKTRACE=1 shows the details; please report it at https://github.com/klad-app/klad/issues",
+ what.replace('\n', " ")
+ )
+}
+
fn run() -> Result<()> {
match Cli::parse().command {
Command::New {
diff --git a/crates/cli/tests/binary.rs b/crates/cli/tests/binary.rs
index bc2dc91..c8cff95 100644
--- a/crates/cli/tests/binary.rs
+++ b/crates/cli/tests/binary.rs
@@ -42,3 +42,30 @@ fn a_drawing_moves_and_a_layer_duplicates_from_the_command_line() {
assert_eq!(starts(&layers[1]), [2, 10]);
std::fs::remove_dir_all(&dir).unwrap();
}
+
+#[test]
+fn an_error_is_one_line_and_the_backtrace_waits_for_klad_backtrace() {
+ let missing = std::env::temp_dir().join(format!("draw-binary-missing-{}.draw", std::process::id()));
+ let run = |backtrace: Option<&str>| {
+ let mut command = Command::new(env!("CARGO_BIN_EXE_klad"));
+ command
+ .args(["ls", missing.to_str().unwrap()])
+ .env("RUST_BACKTRACE", "1");
+ match backtrace {
+ Some(v) => command.env("KLAD_BACKTRACE", v),
+ None => command.env_remove("KLAD_BACKTRACE"),
+ };
+ let out = command.output().unwrap();
+ assert_eq!(out.status.code(), Some(1));
+ String::from_utf8_lossy(&out.stderr).to_string()
+ };
+ let plain = run(None);
+ assert_eq!(plain.lines().count(), 1, "{plain}");
+ assert!(
+ plain.starts_with("error: reading ") && plain.contains("doc.json: "),
+ "{plain}"
+ );
+ assert_eq!(run(Some("0")), plain);
+ let traced = run(Some("1"));
+ assert!(traced.contains("Caused by") && traced.contains("backtrace"), "{traced}");
+}
diff --git a/docs/DESIGN.md b/docs/DESIGN.md
index de59169..3e176d5 100644
--- a/docs/DESIGN.md
+++ b/docs/DESIGN.md
@@ -1,4 +1,4 @@
-_Last updated: 2026-11-04 15:05_
+_Last updated: 2026-11-04 18:46_
# klad: design
@@ -343,6 +343,8 @@ A brush is JSON. The library is built in (`crates/core/brushes/library/`: `ink`
Stateless: read the bundle, apply, write atomically, optionally render. Output is short text by default, `--json` for structure.
+An error is one line on stderr (`error: reading a.draw/doc.json: No such file or directory`) and exit code 1, whatever `RUST_BACKTRACE` says; a bug (a panic) is one line too, with where it happened and where to report it. `KLAD_BACKTRACE=1` prints the whole chain and the backtrace instead.
+
| Command | Purpose |
|---|---|
| `klad new <bundle> [--width --height --layers --background --fps --frames --sketch]` | create (`--sketch` adds a vector layer named sketch on top); the editor's New makes bundles with the same code |