Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error reporting #175

Merged
merged 3 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/controllers/nova_error_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

not_found(Req) ->
%% Check the accept-headers


case cowboy_req:header(<<"accept">>, Req) of
<<"application/json">> ->
%% Render a json response
Expand All @@ -20,14 +18,34 @@ not_found(Req) ->
title => "404 Not found",
message => "We could not find the page you were looking for"},
{ok, Body} = nova_error_dtl:render(Variables),
{status, 404, #{<<"content-type">> => <<"application/json">>}, Body}
{status, 404, #{<<"content-type">> => <<"text/html">>}, Body}
end.

server_error(#{crash_info := #{status_code := StatusCode} = CrashInfo} = Req) ->
Variables = #{status => maps:get(status, CrashInfo, undefined),
title => maps:get(title, CrashInfo, undefined),
message => maps:get(message, CrashInfo, undefined),
extra_msg => maps:get(extra_msg, CrashInfo, undefined),
stacktrace => maps:get(stacktrace, CrashInfo, undefined)},
case application:get_env(nova, render_error_pages, true) of
true ->
case cowboy_req:header(<<"accept">>, Req) of
<<"application/json">> ->
{ok, JsonLib} = nova:get_env(json_lib, thoas),
Json = erlang:apply(JsonLib, encode, [Variables]),
{status, StatusCode, #{<<"content-type">> => <<"application/json">>}, Json};
_ ->
{ok, Body} = nova_error_dtl:render(Variables),
{status, StatusCode, #{<<"content-type">> => <<"text/html">>}, Body}
end;
_ ->
{status, StatusCode}
end;
server_error(#{crash_info := #{stacktrace := Stacktrace, class := Class, reason := Reason}} = Req) ->
Variables = #{status => "Internal Server Error",
title => "500 Internal Server Error",
message => "Something internal crashed. Please take a look!",
extra_msg => io_lib:format("~p, ~p", [Class, Reason]),
extra_msg => io_lib:format("Class: ~p<br /> Reason: ~p", [Class, Reason]),
stacktrace => Stacktrace},

case nova:get_environment() of
Expand Down
47 changes: 30 additions & 17 deletions src/nova_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,37 @@ execute(Req, Env = #{cowboy_handler := Handler, arguments := Arguments}) ->
render_response(Req#{crash_info => Payload}, Env, 500)
end;
execute(Req, Env = #{module := Module, function := Function}) ->
try erlang:apply(Module, Function, [Req]) of
RetObj ->
case nova_handlers:get_handler(element(1, RetObj)) of
{ok, Callback} ->
{ok, Req0} = Callback(RetObj, {Module, Function}, Req),
render_response(Req0, Env);
{error, not_found} ->
logger:error(#{msg => "Controller returned unsupported result", controller => Module,
function => Function, return => RetObj})
end
catch Class:Reason:Stacktrace ->
logger:error(#{msg => "Controller crashed", class => Class, reason => Reason, stacktrace => Stacktrace}),
terminate(Reason, Req, Module),
%% Build the payload object
case erlang:function_exported(Module, Function, 1) of
true ->
try erlang:apply(Module, Function, [Req]) of
RetObj ->
case nova_handlers:get_handler(element(1, RetObj)) of
{ok, Callback} ->
{ok, Req0} = Callback(RetObj, {Module, Function}, Req),
render_response(Req0, Env);
{error, not_found} ->
logger:error(#{msg => "Controller returned unsupported result", controller => Module,
function => Function, return => RetObj})
end
catch Class:Reason:Stacktrace ->
logger:error(#{msg => "Controller crashed", class => Class, reason => Reason, stacktrace => Stacktrace}),
terminate(Reason, Req, Module),
%% Build the payload object
Payload = #{status_code => 500,
stacktrace => Stacktrace,
class => Class,
reason => Reason},
render_response(Req#{crash_info => Payload}, Env, 500)
end;
_ ->
logger:error(#{msg => "Could not find controller", controller => Module, function => io_lib:format("~s/1", [Function])}),
Payload = #{status_code => 500,
stacktrace => Stacktrace,
class => Class,
reason => Reason},
status => "Problems with application",
stacktrace => [{Module, Function, 1}],
title => "Woops. It seems like you have a problem with your application!",
extra_msg => "Nova could not find your controller:function. Pleae check your spelling and/or that the module" ++
" have been properly loaded. "
},
render_response(Req#{crash_info => Payload}, Env, 500)
end.

Expand Down
8 changes: 4 additions & 4 deletions src/views/nova_error.dtl
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@
<span>
<h1>{{ title }}</h1>
{{ message }}
{% if stacktrace %}
{% if extra_msg %}
<pre>
{% if extra_msg %}
{{ extra_msg }}
{% endif %}
{{ extra_msg|safe }}
{% if stacktrace %}
<code class="erlang">
{% for error in stacktrace %}
{{ error }}
{% endfor %}
</code>
{% endif %}
</pre>
{% endif %}
</span>
Expand Down