Skip to content

Commit 9ff7a06

Browse files
committed
feat: replace panics with user-friendly errors
Closes #4
1 parent 908a2ef commit 9ff7a06

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

src/main.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ OPTIONS:
2020
-h, --help Show this help message
2121
";
2222

23+
fn report_template_error(err: minijinja::Error) -> ! {
24+
eprint!("error: {err}");
25+
let debug_info = err.display_debug_info();
26+
let debug_info = debug_info.to_string();
27+
if !debug_info.is_empty() {
28+
eprint!("\n\n{debug_info}");
29+
}
30+
eprintln!();
31+
process::exit(1);
32+
}
33+
2334
fn main() {
2435
let mut args = pico_args::Arguments::from_env();
2536

@@ -33,8 +44,11 @@ fn main() {
3344
.opt_free_from_str::<String>()
3445
.expect("Failed to parse arguments");
3546

36-
let template = if let Some(path) = template_path {
37-
fs::read_to_string(&path).expect("Failed to read template file")
47+
let template = if let Some(ref path) = template_path {
48+
fs::read_to_string(path).unwrap_or_else(|err| {
49+
eprintln!("error: {err} (path: {path})");
50+
process::exit(1);
51+
})
3852
} else {
3953
let mut input = String::new();
4054
io::stdin()
@@ -44,18 +58,23 @@ fn main() {
4458
};
4559

4660
let mut env = Environment::new();
61+
env.set_debug(true);
4762
if strict {
4863
env.set_undefined_behavior(UndefinedBehavior::Strict);
4964
}
5065

51-
env.add_template("template", &template)
52-
.expect("Failed to add template");
66+
if let Err(err) = env.add_template("template", &template) {
67+
report_template_error(err);
68+
}
5369

5470
let tmpl = env
5571
.get_template("template")
5672
.expect("Failed to get template");
5773
let ctx = env::vars().collect::<HashMap<_, _>>();
5874

59-
let output = tmpl.render(&ctx).expect("Failed to render template");
75+
let output = match tmpl.render(&ctx) {
76+
Ok(output) => output,
77+
Err(err) => report_template_error(err),
78+
};
6079
println!("{output}");
6180
}

tests/integration_test.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ fn cargo_run() -> Command {
77
Command::new(env!("CARGO_BIN_EXE_rsubst"))
88
}
99

10-
fn stderr_filters() -> Vec<(&'static str, &'static str)> {
11-
vec![
12-
(r"thread '.*' \(\d+\)", "[thread]"),
13-
(r"src/main\.rs:\d+:\d+", "[file]"),
14-
]
15-
}
16-
1710
#[test]
1811
fn test_simple_output() {
1912
let dir = tempdir().unwrap();
@@ -80,11 +73,7 @@ fn test_missing_template_file() {
8073

8174
assert!(!output.status.success());
8275
let stderr = String::from_utf8(output.stderr).unwrap();
83-
insta::with_settings!({
84-
filters => stderr_filters()
85-
}, {
86-
insta::assert_snapshot!(stderr);
87-
});
76+
insta::assert_snapshot!(stderr);
8877
}
8978

9079
#[test]
@@ -116,11 +105,7 @@ fn test_strict_errors_on_missing_variable() {
116105

117106
assert!(!output.status.success());
118107
let stderr = String::from_utf8(output.stderr).unwrap();
119-
insta::with_settings!({
120-
filters => stderr_filters()
121-
}, {
122-
insta::assert_snapshot!(stderr);
123-
});
108+
insta::assert_snapshot!(stderr);
124109
}
125110

126111
#[test]

tests/snapshots/integration_test__missing_template_file.snap

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22
source: tests/integration_test.rs
33
expression: stderr
44
---
5-
6-
[thread] panicked at [file]:
7-
Failed to read template file: Os { code: 2, kind: NotFound, message: "No such file or directory" }
8-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
error: No such file or directory (os error 2) (path: nonexistent.j2)

tests/snapshots/integration_test__strict_errors_on_missing_variable.snap

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
source: tests/integration_test.rs
33
expression: stderr
44
---
5+
error: undefined value (in template:1)
56

6-
[thread] panicked at [file]:
7-
Failed to render template: Error { kind: UndefinedError, name: "template", line: 1 }
87

98
---------------------------------- template -----------------------------------
109
1 > Hello {{NAME}}!
1110
i ^^^^ undefined value
1211
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1312
No referenced variables
1413
-------------------------------------------------------------------------------
15-
16-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 commit comments

Comments
 (0)