Skip to content

Commit 0600d37

Browse files
authored
Merge pull request #51 from gasche/better-errors
Better errors
2 parents 1c7612e + fb55977 commit 0600d37

File tree

8 files changed

+302
-141
lines changed

8 files changed

+302
-141
lines changed

CHANGES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
### 3.2.0
22

3-
* Improve parsing errors by adding locations (@gasche, #47)
3+
* Improve error messages (@gasche, #47 and #51)
4+
Note: the exceptions raised by Mustache have changed, this breaks
5+
compatibility for users that would catch and deconstruct existing
6+
exceptions.
47
* Add `render_buf` to render templates directly to buffers (@gasche, #48)
58
* When a lookup fails in the current context, lookup in parents contexts.
69
This should fix errors when using "{{#foo}}" for a scalar variable

bin/mustache_cli.ml

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
let apply_mustache json_data template_data =
1+
module Mustache = struct
2+
include Mustache
3+
include With_locations
4+
end
5+
6+
let apply_mustache ~json_data ~template_filename ~template_data =
27
let env = Ezjsonm.from_string json_data
38
and tmpl =
4-
try Mustache.of_string template_data
5-
with Mustache.Parse_error err ->
6-
Format.eprintf "%a@." Mustache.pp_error err;
9+
let lexbuf = Lexing.from_string template_data in
10+
let () =
11+
let open Lexing in
12+
lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = template_filename };
13+
in
14+
try Mustache.parse_lx lexbuf
15+
with Mustache.Template_parse_error err ->
16+
Format.eprintf "Template parse error:@\n%a@."
17+
Mustache.pp_template_parse_error err;
718
exit 3
819
in
9-
Mustache.render tmpl env |> print_endline
20+
try Mustache.render tmpl env |> print_endline
21+
with Mustache.Render_error err ->
22+
Format.eprintf "Template render error:@\n%a@."
23+
Mustache.pp_render_error err;
24+
exit 2
1025

1126
let load_file f =
1227
let ic = open_in f in
@@ -17,10 +32,10 @@ let load_file f =
1732
(Bytes.to_string s)
1833

1934
let run json_filename template_filename =
20-
let j = load_file json_filename
21-
and t = load_file template_filename
35+
let json_data = load_file json_filename
36+
and template_data = load_file template_filename
2237
in
23-
apply_mustache j t
38+
apply_mustache ~json_data ~template_filename ~template_data
2439

2540
let usage () =
2641
print_endline "Usage: mustache-cli json_filename template_filename"

bin/test/errors/parsing-errors.t

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,73 @@ Delimiter problems:
44
$ PROBLEM=no-closing-mustache.mustache
55
$ echo "{{foo" > $PROBLEM
66
$ mustache foo.json $PROBLEM
7-
Line 2, character 0: syntax error.
7+
Template parse error:
8+
File "no-closing-mustache.mustache", line 2, character 0: syntax error.
89
[3]
910
1011
$ PROBLEM=one-closing-mustache.mustache
1112
$ echo "{{foo}" > $PROBLEM
1213
$ mustache foo.json $PROBLEM
13-
Lines 1-2, characters 6-0: syntax error.
14+
Template parse error:
15+
File "one-closing-mustache.mustache", lines 1-2, characters 6-0:
16+
syntax error.
1417
[3]
1518
1619
$ PROBLEM=eof-before-variable.mustache
1720
$ echo "{{" > $PROBLEM
1821
$ mustache foo.json $PROBLEM
19-
Line 2, character 0: ident expected.
22+
Template parse error:
23+
File "eof-before-variable.mustache", line 2, character 0: ident expected.
2024
[3]
2125
2226
$ PROBLEM=eof-before-section.mustache
2327
$ echo "{{#" > $PROBLEM
2428
$ mustache foo.json $PROBLEM
25-
Line 2, character 0: ident expected.
29+
Template parse error:
30+
File "eof-before-section.mustache", line 2, character 0: ident expected.
2631
[3]
2732
2833
$ PROBLEM=eof-before-section-end.mustache
2934
$ echo "{{#foo}} {{.}} {{/" > $PROBLEM
3035
$ mustache foo.json $PROBLEM
31-
Line 2, character 0: ident expected.
36+
Template parse error:
37+
File "eof-before-section-end.mustache", line 2, character 0: ident expected.
3238
[3]
3339
3440
$ PROBLEM=eof-before-inverted-section.mustache
3541
$ echo "{{^" > $PROBLEM
3642
$ mustache foo.json $PROBLEM
37-
Line 2, character 0: ident expected.
43+
Template parse error:
44+
File "eof-before-inverted-section.mustache", line 2, character 0:
45+
ident expected.
3846
[3]
3947
4048
$ PROBLEM=eof-before-unescape.mustache
4149
$ echo "{{{" > $PROBLEM
4250
$ mustache foo.json $PROBLEM
43-
Line 2, character 0: ident expected.
51+
Template parse error:
52+
File "eof-before-unescape.mustache", line 2, character 0: ident expected.
4453
[3]
4554
4655
$ PROBLEM=eof-before-unescape.mustache
4756
$ echo "{{&" > $PROBLEM
4857
$ mustache foo.json $PROBLEM
49-
Line 2, character 0: ident expected.
58+
Template parse error:
59+
File "eof-before-unescape.mustache", line 2, character 0: ident expected.
5060
[3]
5161
5262
$ PROBLEM=eof-before-partial.mustache
5363
$ echo "{{>" > $PROBLEM
5464
$ mustache foo.json $PROBLEM
55-
Line 2, character 0: ident expected.
65+
Template parse error:
66+
File "eof-before-partial.mustache", line 2, character 0: ident expected.
5667
[3]
5768
5869
$ PROBLEM=eof-in-comment.mustache
5970
$ echo "{{! non-terminated comment" > $PROBLEM
6071
$ mustache foo.json $PROBLEM
61-
Line 2, character 0: non-terminated comment.
72+
Template parse error:
73+
File "eof-in-comment.mustache", line 2, character 0: non-terminated comment.
6274
[3]
6375
6476
@@ -67,13 +79,15 @@ Mismatches between opening and closing mustaches:
6779
$ PROBLEM=two-three.mustache
6880
$ echo "{{ foo }}}" > $PROBLEM
6981
$ mustache foo.json $PROBLEM
70-
Lines 1-2, characters 10-0: syntax error.
82+
Template parse error:
83+
File "two-three.mustache", lines 1-2, characters 10-0: syntax error.
7184
[3]
7285
7386
$ PROBLEM=three-two.mustache
7487
$ echo "{{{ foo }}" > $PROBLEM
7588
$ mustache foo.json $PROBLEM
76-
Lines 1-2, characters 10-0: syntax error.
89+
Template parse error:
90+
File "three-two.mustache", lines 1-2, characters 10-0: syntax error.
7791
[3]
7892
7993
@@ -82,17 +96,22 @@ Mismatch between section-start and section-end:
8296
$ PROBLEM=foo-bar.mustache
8397
$ echo "{{#foo}} {{.}} {{/bar}}" > $PROBLEM
8498
$ mustache foo.json $PROBLEM
85-
Fatal error: exception Mustache_types.Invalid_template("Mismatched section foo with bar")
86-
[2]
99+
Template parse error:
100+
File "foo-bar.mustache", lines 1-2, characters 23-0:
101+
Section mismatch: {{#foo}} is closed by {{/bar}}.
102+
[3]
87103
88104
$ PROBLEM=foo-not-closed.mustache
89105
$ echo "{{#foo}} {{.}} {{foo}}" > $PROBLEM
90106
$ mustache foo.json $PROBLEM
91-
Line 2, character 0: syntax error.
107+
Template parse error:
108+
File "foo-not-closed.mustache", line 2, character 0: syntax error.
92109
[3]
93110
94111
$ PROBLEM=wrong-nesting.mustache
95112
$ echo "{{#bar}} {{#foo}} {{.}} {{/bar}} {{/foo}}" > $PROBLEM
96113
$ mustache foo.json $PROBLEM
97-
Fatal error: exception Mustache_types.Invalid_template("Mismatched section foo with bar")
98-
[2]
114+
Template parse error:
115+
File "wrong-nesting.mustache", lines 1-2, characters 41-0:
116+
Section mismatch: {{#foo}} is closed by {{/bar}}.
117+
[3]

bin/test/errors/render-errors.t/run.t

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,69 @@ one possible source of error, or both, or none.
2727
Invalid variable name:
2828

2929
$ mustache reference.json missing-variable.mustache
30-
Fatal error: exception Mustache_types.Missing_variable("na")
30+
Template render error:
31+
File "missing-variable.mustache", line 14, characters 40-46:
32+
the variable 'na' is missing.
3133
[2]
3234

3335
$ mustache missing-variable.json reference.mustache
34-
Fatal error: exception Mustache_types.Missing_variable("data")
36+
Template render error:
37+
File "reference.mustache", line 5, characters 4-12:
38+
the variable 'data' is missing.
3539
[2]
3640

3741
Invalid section name:
3842

3943
$ mustache reference.json missing-section.mustache
40-
Fatal error: exception Mustache_types.Missing_section("na")
44+
Template render error:
45+
File "missing-section.mustache", line 14, characters 0-55:
46+
the section 'na' is missing.
4147
[2]
4248

4349
$ mustache missing-section.json reference.mustache
44-
Fatal error: exception Mustache_types.Missing_section("group")
50+
Template render error:
51+
File "reference.mustache", lines 9-12, characters 0-10:
52+
the section 'group' is missing.
4553
[2]
4654

4755
Error in a dotted path foo.bar (one case for the first component, the other in the second).
4856

4957
$ mustache reference.json invalid-dotted-name-1.mustache
50-
Fatal error: exception Mustache_types.Missing_variable("gro")
58+
Template render error:
59+
File "invalid-dotted-name-1.mustache", line 10, characters 2-15:
60+
the variable 'gro' is missing.
5161
[2]
5262

5363
$ mustache invalid-dotted-name-1.json reference.mustache
54-
Fatal error: exception Mustache_types.Missing_section("group")
64+
Template render error:
65+
File "reference.mustache", lines 9-12, characters 0-10:
66+
the section 'group' is missing.
5567
[2]
5668

5769
$ mustache reference.json invalid-dotted-name-2.mustache
58-
Fatal error: exception Mustache_types.Missing_variable("fir")
70+
Template render error:
71+
File "invalid-dotted-name-2.mustache", line 10, characters 2-15:
72+
the variable 'group.fir' is missing.
5973
[2]
6074

6175
$ mustache invalid-dotted-name-2.json reference.mustache
62-
Fatal error: exception Mustache_types.Missing_variable("first")
76+
Template render error:
77+
File "reference.mustache", line 10, characters 2-17:
78+
the variable 'group.first' is missing.
6379
[2]
6480

6581
Non-scalar used as a scalar:
6682

6783
$ mustache reference.json non-scalar.mustache
68-
Fatal error: exception Mustache_types.Invalid_param("Lookup.scalar: not a scalar")
84+
Template render error:
85+
File "non-scalar.mustache", line 4, characters 0-8:
86+
the value of 'list' is not a valid scalar.
6987
[2]
7088

7189
$ mustache non-scalar.json reference.mustache
72-
Fatal error: exception Mustache_types.Invalid_param("Lookup.scalar: not a scalar")
90+
Template render error:
91+
File "reference.mustache", line 1, characters 7-16:
92+
the value of 'title' is not a valid scalar.
7393
[2]
7494

7595
Missing partial (currently the CLI does not support any partial anyway):
@@ -78,5 +98,7 @@ Missing partial (currently the CLI does not support any partial anyway):
7898
in better `ls` output).
7999

80100
$ mustache reference.json z-missing-partial.mustache
81-
Fatal error: exception Mustache_types.Missing_partial("second")
101+
Template render error:
102+
File "z-missing-partial.mustache", line 11, characters 2-13:
103+
the partial 'second' is missing.
82104
[2]

0 commit comments

Comments
 (0)