Merge pull request #7802 from dotCooCoo/fix/text-a-script-throws-itself-is-not-translated

Scripts: the status bar shows text a script throws as it is
This commit is contained in:
BFlat
2026-11-19 00:39:21 +07:00
committed by GitHub
14 changed files with 342 additions and 33 deletions
+25
View File
@@ -77,6 +77,31 @@ fn script_missing_include_reports_origin() {
assert!(report["message"].as_str().unwrap().contains("include file not found: nope.jsxinc"), "{report}");
assert_eq!(report["log"], json!([]), "nothing ran before the include was read");
assert!(report.get("file").is_none(), "the line is the script's own");
assert_eq!(report["messageOrigin"], "built-in");
let _ = std::fs::remove_dir_all(base);
}
/// An error in an included file keeps where its message came from: a value the file threw is the
/// script's own text, and an error the app raised for it, or for a file `$.evalFile` could not run, is not.
#[test]
fn script_include_errors_keep_their_message_origin() {
let base = folder("origin");
write(&base.join("main/lib.jsxinc"), "var a = 1;\n throw new RangeError('invalid array length');");
write(&base.join("main/native.jsxinc"), "var a = 1;\n new Array(-1);");
for (text, message, origin) in [
("#include \"lib.jsxinc\"", "RangeError: invalid array length", "script"),
("#include \"native.jsxinc\"", "RangeError: invalid array length", "built-in"),
("try {\n#include \"native.jsxinc\"\n} catch (e) { throw e; }", "RangeError: invalid array length", "built-in"),
("try {\n#include \"native.jsxinc\"\n} catch (e) { throw e.message; }", "invalid array length", "script"),
] {
let report = failed(&base, text);
assert_eq!((report["message"].as_str(), report["messageOrigin"].as_str()), (Some(message), Some(origin)), "{text}: {report}");
}
let report = failed(&base, "#include \"lib.jsxinc\"");
let file = report["file"].as_str().map(|f| Path::new(f).ends_with("lib.jsxinc"));
assert_eq!((file, report["line"].as_u64(), report["column"].as_u64()), (Some(true), Some(2), Some(3)), "{report}");
let report = failed(&base, "$.evalFile('nope.jsx');");
assert_eq!(report["messageOrigin"], "built-in", "{report}");
let _ = std::fs::remove_dir_all(base);
}
+52
View File
@@ -170,6 +170,58 @@ fn an_error_names_the_script_its_place_and_kind() {
assert_eq!(s.script_report().unwrap()["line"], 1);
}
/// A failed run's report keeps its message and kind and says where the message came from: text the
/// script supplied, or one of the app's own errors. The status text and the console line read the
/// same either way, and only the app's own errors get advice about other applications' objects.
#[test]
fn failed_script_report_keeps_message_and_adds_origin() {
let mut s = two_docs();
for (source, kind, message, origin) in [
("throw new Error('invalid array length');", "thrown", "Error: invalid array length", "script"),
("throw new RangeError('invalid array length');", "thrown", "RangeError: invalid array length", "script"),
("throw 'invalid array length';", "thrown", "invalid array length", "script"),
("throw 'ReferenceError: app is not defined';", "thrown", "ReferenceError: app is not defined", "script"),
("try { new Array(-1); } catch (e) { throw new Error(e.message); }", "thrown", "Error: invalid array length", "script"),
("new Array(-1);", "thrown", "RangeError: invalid array length", "built-in"),
("try { new Array(-1); } catch (e) { throw e; }", "thrown", "RangeError: invalid array length", "built-in"),
("vc.run();", "thrown", "TypeError: vc.run: give a command id, as a string", "built-in"),
] {
let e = run(&mut s, source).unwrap_err().to_string();
let report = s.script_report().unwrap().clone();
assert_eq!(
(report["kind"].as_str(), report["message"].as_str(), report["messageOrigin"].as_str()),
(Some(kind), Some(message), Some(origin)),
"{source}"
);
let (line, column) = (&report["line"], &report["column"]);
let before = format!("t.js: line {line}, column {column}: ");
assert_eq!(e, format!("{before}{message} ({kind})"), "{source}");
assert_eq!(s.script_failure_text().as_deref(), Some(e.as_str()));
let authored = s.script_authored_failure();
assert_eq!(authored, (origin == "script").then(|| (before.clone(), message, format!(" ({kind})"))), "{source}");
let console = s.execute("script.console", &json!({})).unwrap();
let error = console["lines"].as_array().unwrap().iter().rev().find(|l| l["kind"] == "error").map(|l| l["text"].clone());
assert_eq!(error, Some(json!(format!("t.js:{line}:{column}: {message}"))), "{source}");
}
// The app's own reference error names the `vc` object; the same text a script threw does not.
run(&mut s, "app.activeDocument;").unwrap_err();
assert!(s.script_report().unwrap()["message"].as_str().unwrap().contains("`vc`"));
assert_eq!(s.script_report().unwrap()["messageOrigin"], "built-in");
// An error in code `eval` compiled keeps where its message came from.
let e = run(&mut s, "eval(\"throw new Error('invalid array length')\");").unwrap_err().to_string();
assert_eq!(e, "t.js: Error: invalid array length (thrown)");
assert_eq!(s.script_authored_failure(), Some(("t.js: ".to_string(), "Error: invalid array length", " (thrown)".to_string())));
// Syntax errors and limits are the app's own.
run(&mut s, "var = 1;").unwrap_err();
assert_eq!((s.script_report().unwrap()["kind"].as_str(), s.script_authored_failure()), (Some("syntax"), None));
s.execute("script.run", &json!({"source": "while (true) {}", "name": "t.js", "maxFuel": 100_000})).unwrap_err();
assert_eq!((s.script_report().unwrap()["messageOrigin"].as_str(), s.script_authored_failure()), (Some("built-in"), None));
// A run that completes has no failure to split.
run(&mut s, "throw 'x';").unwrap_err();
run(&mut s, "1").unwrap();
assert_eq!(s.script_authored_failure(), None);
}
/// An error raised where the run has no place for it, such as in code `eval` compiled, is named
/// with the script and its kind alone, with one colon after the script's name.
#[test]