Skip to content

Commit

Permalink
Merge pull request #51 from gasche/better-errors
Browse files Browse the repository at this point in the history
Better errors
  • Loading branch information
gasche authored Dec 27, 2020
2 parents 1c7612e + fb55977 commit 0600d37
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 141 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
### 3.2.0

* Improve parsing errors by adding locations (@gasche, #47)
* Improve error messages (@gasche, #47 and #51)
Note: the exceptions raised by Mustache have changed, this breaks
compatibility for users that would catch and deconstruct existing
exceptions.
* Add `render_buf` to render templates directly to buffers (@gasche, #48)
* When a lookup fails in the current context, lookup in parents contexts.
This should fix errors when using "{{#foo}}" for a scalar variable
Expand Down
31 changes: 23 additions & 8 deletions bin/mustache_cli.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
let apply_mustache json_data template_data =
module Mustache = struct
include Mustache
include With_locations
end

let apply_mustache ~json_data ~template_filename ~template_data =
let env = Ezjsonm.from_string json_data
and tmpl =
try Mustache.of_string template_data
with Mustache.Parse_error err ->
Format.eprintf "%a@." Mustache.pp_error err;
let lexbuf = Lexing.from_string template_data in
let () =
let open Lexing in
lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = template_filename };
in
try Mustache.parse_lx lexbuf
with Mustache.Template_parse_error err ->
Format.eprintf "Template parse error:@\n%a@."
Mustache.pp_template_parse_error err;
exit 3
in
Mustache.render tmpl env |> print_endline
try Mustache.render tmpl env |> print_endline
with Mustache.Render_error err ->
Format.eprintf "Template render error:@\n%a@."
Mustache.pp_render_error err;
exit 2

let load_file f =
let ic = open_in f in
Expand All @@ -17,10 +32,10 @@ let load_file f =
(Bytes.to_string s)

let run json_filename template_filename =
let j = load_file json_filename
and t = load_file template_filename
let json_data = load_file json_filename
and template_data = load_file template_filename
in
apply_mustache j t
apply_mustache ~json_data ~template_filename ~template_data

let usage () =
print_endline "Usage: mustache-cli json_filename template_filename"
Expand Down
53 changes: 36 additions & 17 deletions bin/test/errors/parsing-errors.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,73 @@ Delimiter problems:
$ PROBLEM=no-closing-mustache.mustache
$ echo "{{foo" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: syntax error.
Template parse error:
File "no-closing-mustache.mustache", line 2, character 0: syntax error.
[3]
$ PROBLEM=one-closing-mustache.mustache
$ echo "{{foo}" > $PROBLEM
$ mustache foo.json $PROBLEM
Lines 1-2, characters 6-0: syntax error.
Template parse error:
File "one-closing-mustache.mustache", lines 1-2, characters 6-0:
syntax error.
[3]
$ PROBLEM=eof-before-variable.mustache
$ echo "{{" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: ident expected.
Template parse error:
File "eof-before-variable.mustache", line 2, character 0: ident expected.
[3]
$ PROBLEM=eof-before-section.mustache
$ echo "{{#" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: ident expected.
Template parse error:
File "eof-before-section.mustache", line 2, character 0: ident expected.
[3]
$ PROBLEM=eof-before-section-end.mustache
$ echo "{{#foo}} {{.}} {{/" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: ident expected.
Template parse error:
File "eof-before-section-end.mustache", line 2, character 0: ident expected.
[3]
$ PROBLEM=eof-before-inverted-section.mustache
$ echo "{{^" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: ident expected.
Template parse error:
File "eof-before-inverted-section.mustache", line 2, character 0:
ident expected.
[3]
$ PROBLEM=eof-before-unescape.mustache
$ echo "{{{" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: ident expected.
Template parse error:
File "eof-before-unescape.mustache", line 2, character 0: ident expected.
[3]
$ PROBLEM=eof-before-unescape.mustache
$ echo "{{&" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: ident expected.
Template parse error:
File "eof-before-unescape.mustache", line 2, character 0: ident expected.
[3]
$ PROBLEM=eof-before-partial.mustache
$ echo "{{>" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: ident expected.
Template parse error:
File "eof-before-partial.mustache", line 2, character 0: ident expected.
[3]
$ PROBLEM=eof-in-comment.mustache
$ echo "{{! non-terminated comment" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: non-terminated comment.
Template parse error:
File "eof-in-comment.mustache", line 2, character 0: non-terminated comment.
[3]
Expand All @@ -67,13 +79,15 @@ Mismatches between opening and closing mustaches:
$ PROBLEM=two-three.mustache
$ echo "{{ foo }}}" > $PROBLEM
$ mustache foo.json $PROBLEM
Lines 1-2, characters 10-0: syntax error.
Template parse error:
File "two-three.mustache", lines 1-2, characters 10-0: syntax error.
[3]
$ PROBLEM=three-two.mustache
$ echo "{{{ foo }}" > $PROBLEM
$ mustache foo.json $PROBLEM
Lines 1-2, characters 10-0: syntax error.
Template parse error:
File "three-two.mustache", lines 1-2, characters 10-0: syntax error.
[3]
Expand All @@ -82,17 +96,22 @@ Mismatch between section-start and section-end:
$ PROBLEM=foo-bar.mustache
$ echo "{{#foo}} {{.}} {{/bar}}" > $PROBLEM
$ mustache foo.json $PROBLEM
Fatal error: exception Mustache_types.Invalid_template("Mismatched section foo with bar")
[2]
Template parse error:
File "foo-bar.mustache", lines 1-2, characters 23-0:
Section mismatch: {{#foo}} is closed by {{/bar}}.
[3]
$ PROBLEM=foo-not-closed.mustache
$ echo "{{#foo}} {{.}} {{foo}}" > $PROBLEM
$ mustache foo.json $PROBLEM
Line 2, character 0: syntax error.
Template parse error:
File "foo-not-closed.mustache", line 2, character 0: syntax error.
[3]
$ PROBLEM=wrong-nesting.mustache
$ echo "{{#bar}} {{#foo}} {{.}} {{/bar}} {{/foo}}" > $PROBLEM
$ mustache foo.json $PROBLEM
Fatal error: exception Mustache_types.Invalid_template("Mismatched section foo with bar")
[2]
Template parse error:
File "wrong-nesting.mustache", lines 1-2, characters 41-0:
Section mismatch: {{#foo}} is closed by {{/bar}}.
[3]
44 changes: 33 additions & 11 deletions bin/test/errors/render-errors.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,69 @@ one possible source of error, or both, or none.
Invalid variable name:

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

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

Invalid section name:

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

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

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

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

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

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

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

Non-scalar used as a scalar:

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

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

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

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

0 comments on commit 0600d37

Please sign in to comment.