Skip to content

Commit 9df6265

Browse files
committed
use proper locations in mustache_cli and catch render errors
1 parent f06245a commit 9df6265

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

bin/mustache_cli.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
module Mustache = struct
2+
include Mustache
3+
include With_locations
4+
end
5+
16
let apply_mustache json_data template_data =
27
let env = Ezjsonm.from_string json_data
38
and tmpl =
49
try Mustache.of_string template_data
510
with Mustache.Template_parse_error err ->
6-
Format.eprintf "%a@."
11+
Format.eprintf "Template parse error:@\n%a@."
712
Mustache.pp_template_parse_error err;
813
exit 3
914
in
10-
Mustache.render tmpl env |> print_endline
15+
try Mustache.render tmpl env |> print_endline
16+
with Mustache.Render_error err ->
17+
Format.eprintf "Template render error:@\n%a@."
18+
Mustache.pp_render_error err;
19+
exit 2
1120

1221
let load_file f =
1322
let ic = open_in f in

bin/test/errors/parsing-errors.t

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,70 @@ Delimiter problems:
44
$ PROBLEM=no-closing-mustache.mustache
55
$ echo "{{foo" > $PROBLEM
66
$ mustache foo.json $PROBLEM
7+
Template parse error:
78
Line 2, character 0: syntax error.
89
[3]
910
1011
$ PROBLEM=one-closing-mustache.mustache
1112
$ echo "{{foo}" > $PROBLEM
1213
$ mustache foo.json $PROBLEM
14+
Template parse error:
1315
Lines 1-2, characters 6-0: syntax error.
1416
[3]
1517
1618
$ PROBLEM=eof-before-variable.mustache
1719
$ echo "{{" > $PROBLEM
1820
$ mustache foo.json $PROBLEM
21+
Template parse error:
1922
Line 2, character 0: ident expected.
2023
[3]
2124
2225
$ PROBLEM=eof-before-section.mustache
2326
$ echo "{{#" > $PROBLEM
2427
$ mustache foo.json $PROBLEM
28+
Template parse error:
2529
Line 2, character 0: ident expected.
2630
[3]
2731
2832
$ PROBLEM=eof-before-section-end.mustache
2933
$ echo "{{#foo}} {{.}} {{/" > $PROBLEM
3034
$ mustache foo.json $PROBLEM
35+
Template parse error:
3136
Line 2, character 0: ident expected.
3237
[3]
3338
3439
$ PROBLEM=eof-before-inverted-section.mustache
3540
$ echo "{{^" > $PROBLEM
3641
$ mustache foo.json $PROBLEM
42+
Template parse error:
3743
Line 2, character 0: ident expected.
3844
[3]
3945
4046
$ PROBLEM=eof-before-unescape.mustache
4147
$ echo "{{{" > $PROBLEM
4248
$ mustache foo.json $PROBLEM
49+
Template parse error:
4350
Line 2, character 0: ident expected.
4451
[3]
4552
4653
$ PROBLEM=eof-before-unescape.mustache
4754
$ echo "{{&" > $PROBLEM
4855
$ mustache foo.json $PROBLEM
56+
Template parse error:
4957
Line 2, character 0: ident expected.
5058
[3]
5159
5260
$ PROBLEM=eof-before-partial.mustache
5361
$ echo "{{>" > $PROBLEM
5462
$ mustache foo.json $PROBLEM
63+
Template parse error:
5564
Line 2, character 0: ident expected.
5665
[3]
5766
5867
$ PROBLEM=eof-in-comment.mustache
5968
$ echo "{{! non-terminated comment" > $PROBLEM
6069
$ mustache foo.json $PROBLEM
70+
Template parse error:
6171
Line 2, character 0: non-terminated comment.
6272
[3]
6373
@@ -67,12 +77,14 @@ Mismatches between opening and closing mustaches:
6777
$ PROBLEM=two-three.mustache
6878
$ echo "{{ foo }}}" > $PROBLEM
6979
$ mustache foo.json $PROBLEM
80+
Template parse error:
7081
Lines 1-2, characters 10-0: syntax error.
7182
[3]
7283
7384
$ PROBLEM=three-two.mustache
7485
$ echo "{{{ foo }}" > $PROBLEM
7586
$ mustache foo.json $PROBLEM
87+
Template parse error:
7688
Lines 1-2, characters 10-0: syntax error.
7789
[3]
7890
@@ -82,17 +94,20 @@ Mismatch between section-start and section-end:
8294
$ PROBLEM=foo-bar.mustache
8395
$ echo "{{#foo}} {{.}} {{/bar}}" > $PROBLEM
8496
$ mustache foo.json $PROBLEM
97+
Template parse error:
8598
Lines 1-2, characters 23-0: Mismatched section 'foo' with 'bar'.
8699
[3]
87100
88101
$ PROBLEM=foo-not-closed.mustache
89102
$ echo "{{#foo}} {{.}} {{foo}}" > $PROBLEM
90103
$ mustache foo.json $PROBLEM
104+
Template parse error:
91105
Line 2, character 0: syntax error.
92106
[3]
93107
94108
$ PROBLEM=wrong-nesting.mustache
95109
$ echo "{{#bar}} {{#foo}} {{.}} {{/bar}} {{/foo}}" > $PROBLEM
96110
$ mustache foo.json $PROBLEM
111+
Template parse error:
97112
Lines 1-2, characters 41-0: Mismatched section 'foo' with 'bar'.
98113
[3]

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,59 +27,59 @@ 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.Render_error:
31-
Line 0, character -1: the variable 'na' is missing.
30+
Template render error:
31+
Line 14, characters 40-46: the variable 'na' is missing.
3232
[2]
3333

3434
$ mustache missing-variable.json reference.mustache
35-
Fatal error: exception Mustache.Render_error:
36-
Line 0, character -1: the variable 'data' is missing.
35+
Template render error:
36+
Line 5, characters 4-12: the variable 'data' is missing.
3737
[2]
3838

3939
Invalid section name:
4040

4141
$ mustache reference.json missing-section.mustache
42-
Fatal error: exception Mustache.Render_error:
43-
Line 0, character -1: the section 'na' is missing.
42+
Template render error:
43+
Line 14, characters 0-55: the section 'na' is missing.
4444
[2]
4545

4646
$ mustache missing-section.json reference.mustache
47-
Fatal error: exception Mustache.Render_error:
48-
Line 0, character -1: the section 'group' is missing.
47+
Template render error:
48+
Lines 9-12, characters 0-10: the section 'group' is missing.
4949
[2]
5050

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

5353
$ mustache reference.json invalid-dotted-name-1.mustache
54-
Fatal error: exception Mustache.Render_error:
55-
Line 0, character -1: the variable 'gro' is missing.
54+
Template render error:
55+
Line 10, characters 2-15: the variable 'gro' is missing.
5656
[2]
5757

5858
$ mustache invalid-dotted-name-1.json reference.mustache
59-
Fatal error: exception Mustache.Render_error:
60-
Line 0, character -1: the section 'group' is missing.
59+
Template render error:
60+
Lines 9-12, characters 0-10: the section 'group' is missing.
6161
[2]
6262

6363
$ mustache reference.json invalid-dotted-name-2.mustache
64-
Fatal error: exception Mustache.Render_error:
65-
Line 0, character -1: the variable 'group.fir' is missing.
64+
Template render error:
65+
Line 10, characters 2-15: the variable 'group.fir' is missing.
6666
[2]
6767

6868
$ mustache invalid-dotted-name-2.json reference.mustache
69-
Fatal error: exception Mustache.Render_error:
70-
Line 0, character -1: the variable 'group.first' is missing.
69+
Template render error:
70+
Line 10, characters 2-17: the variable 'group.first' is missing.
7171
[2]
7272

7373
Non-scalar used as a scalar:
7474

7575
$ mustache reference.json non-scalar.mustache
76-
Fatal error: exception Mustache.Render_error:
77-
Line 0, character -1: the value of 'list' is not a valid scalar.
76+
Template render error:
77+
Line 4, characters 0-8: the value of 'list' is not a valid scalar.
7878
[2]
7979

8080
$ mustache non-scalar.json reference.mustache
81-
Fatal error: exception Mustache.Render_error:
82-
Line 0, character -1: the value of 'title' is not a valid scalar.
81+
Template render error:
82+
Line 1, characters 7-16: the value of 'title' is not a valid scalar.
8383
[2]
8484

8585
Missing partial (currently the CLI does not support any partial anyway):
@@ -88,6 +88,6 @@ Missing partial (currently the CLI does not support any partial anyway):
8888
in better `ls` output).
8989

9090
$ mustache reference.json z-missing-partial.mustache
91-
Fatal error: exception Mustache.Render_error:
92-
Line 0, character -1: the partial 'second' is missing.
91+
Template render error:
92+
Line 11, characters 2-13: the partial 'second' is missing.
9393
[2]

0 commit comments

Comments
 (0)