Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved template error reporting

  • Loading branch information...
commit 4eea1c599c8bcd7106f2e2e03b93aa17c8ba9fc6 1 parent 9a5605f
Yurii Rashkovskii authored
Showing with 17 additions and 2 deletions.
  1. +5 −1 priv/views/_template_error.html
  2. +12 −1 src/cowboy_controller.erl
View
6 priv/views/_template_error.html
@@ -1,6 +1,10 @@
{% extends "_error.html" %}
{% block error_title %} Template Error {% endblock %}
{% block error %}
- Error while processing template {{ template }} on line #{{ error_line }}:<br />
+ Error while processing template {{ template }} on line #{{ error_line }} (column {{ error_column | default: "unknown" }}):
<b>{{ error }}</b>
+ <h4>Source</h4>
+ <pre>
+ {{ actual_line }}
+ </pre>
{% endblock %}
View
13 src/cowboy_controller.erl
@@ -114,9 +114,16 @@ render_template({Template, Variables}, #state{
{_, ok} ->
{ok, Rendered} = TemplateModule:render(MetaVariables ++ [{erlang_application, App},{action, Action},{controller, Handler}|Variables]),
iolist_to_binary(Rendered);
- {development, {error, {_File, [{Line, erlydtl_scanner, Reason}]}}} ->
+ {development, {error, {File, [{{Line, Col},erlydtl_parser, Reason}]}}} ->
render_template({views_filename("_template_error.html", CtrlPriv),
[{template, Template}, {variables, MetaVariables ++ Variables},
+ {error_line, Line}, {error_column, Col},
+ {actual_line, read_line(File, Line)},
+ {error, Reason}]}, State);
+ {development, {error, {File, [{Line, erlydtl_scanner, Reason}]}}} ->
+ render_template({views_filename("_template_error.html", CtrlPriv),
+ [{template, Template}, {variables, MetaVariables ++ Variables},
+ {actual_line, read_line(File, Line)},
{error_line, Line}, {error, Reason}]}, State);
{production, _} ->
render_template({views_filename("_public_error.html", CtrlPriv), []}, State)
@@ -129,3 +136,7 @@ views_filename(Path, Priv) ->
helpers_filename(Priv) ->
filename:join([Priv,"helpers"]).
+
+read_line(File, Line) ->
+ {ok, B} = file:read_file(File),
+ lists:nth(Line, binary:split(B, <<$\n>>, [global,trim])).
Please sign in to comment.
Something went wrong with that request. Please try again.