Skip to content

Commit

Permalink
Improve error message when returning non Lucky::Response
Browse files Browse the repository at this point in the history
Closes #460
Closes #1037
  • Loading branch information
paulcsmith committed Apr 5, 2020
1 parent 85b608a commit c50cf85
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions src/lucky/renderable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,66 @@ module Lucky::Renderable
response.print
end

private def handle_response(_response : Nil) forall T
# This is a hack to force Crystal to regenerate this method for
# each action. That way @type refers to the action with the problem,
# and not the first action to be compiled.
{% @type %}
{%
raise <<-ERROR
#{@type} returned Nil
But it should return a Lucky::Response.
Try this...
▸ Ensure the action returns a response with html/redirect/json/etc.
▸ Ensure all conditionals (like if/else) return a response with html/redirect/json/etc.
For example...
get "/admin/users" do
# Make sure there is a response in all conditional branches
if current_user.admin?
html IndexPage, users: UserQuery.new
else
redirect Home::Index
end
end
ERROR
%}
end

private def handle_response(_response : T) forall T
# This is a hack to force Crystal to regenerate this method for
# each action. That way @type refers to the action with the problem,
# and not the first action to be compiled.
{% @type %}
{%
raise <<-ERROR
Your action returned #{T}
#{@type} returned #{T}
But it should return a Lucky::Response
Try this...
▸ Use a method like render/redirect/json at the end of your action.
▸ Ensure all conditionals (like if/else) return a response with render/redirect/json/etc.
▸ Use a method like html/redirect/json at the end of your action.
▸ Ensure all conditionals (like if/else) return a response with html/redirect/json/etc.
For example...
get "/users" do
# Render html, json, or redirect
html IndexPage, users: UserQuery.new
redirect to: Home::Index
json({name: "James"})
end
ERROR
%}
end
Expand Down

0 comments on commit c50cf85

Please sign in to comment.