1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use html for DidYouMean suggestions on error pages

The current suggestions are shown on a single line.
Instead we can use some prettier formatting.
This commit is contained in:
Petrik 2020-05-20 09:55:30 +02:00
parent f6a94aff2e
commit 5cf725acb6
4 changed files with 46 additions and 8 deletions

View file

@ -0,0 +1,18 @@
<% if exception.respond_to?(:original_message) && exception.respond_to?(:corrections) %>
<h2><%= h exception.original_message %></h2>
<%
# The 'did_you_mean' gem can raise exceptions when calling #corrections on
# the exception. If it does there are no corrections to show.
corrections = exception.corrections rescue []
%>
<% if corrections.any? %>
<b>Did you mean?</b>
<ul>
<% corrections.each do |correction| %>
<li style="list-style-type: none"><%= h correction %></li>
<% end %>
</ul>
<% end %>
<% else %>
<h2><%= h exception.message %></h2>
<% end %>

View file

@ -8,11 +8,8 @@
</header>
<div id="container">
<h2>
<%= h @exception.message %>
<%= render "rescues/actions", exception: @exception, request: @request %>
</h2>
<%= render "rescues/message_and_suggestions", exception: @exception %>
<%= render "rescues/actions", exception: @exception, request: @request %>
<%= render "rescues/source", source_extracts: @source_extracts, show_source_idx: @show_source_idx, error_index: 0 %>
<%= render "rescues/trace", traces: @traces, trace_to_show: @trace_to_show, error_index: 0 %>

View file

@ -2,5 +2,5 @@
<h1>Unknown action</h1>
</header>
<div id="container">
<h2><%= h @exception.message %></h2>
<%= render "rescues/message_and_suggestions", exception: @exception %>
</div>

View file

@ -17,6 +17,12 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
end
end
class SimpleController < ActionController::Base
def hello
self.response_body = "hello"
end
end
class Boomer
attr_accessor :closed
@ -67,7 +73,8 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
when "/pass"
[404, { "X-Cascade" => "pass" }, self]
when "/not_found"
raise AbstractController::ActionNotFound
controller = SimpleController.new
raise AbstractController::ActionNotFound.new(nil, controller, :not_found)
when "/runtime_error"
raise RuntimeError
when "/method_not_allowed"
@ -101,7 +108,7 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
when "/missing_keys"
raise ActionController::UrlGenerationError, "No route matches"
when "/parameter_missing"
raise ActionController::ParameterMissing, :missing_param_key
raise ActionController::ParameterMissing.new(:missing_param_key, %w(valid_param_key))
when "/original_syntax_error"
eval "broke_syntax =" # `eval` need for raise native SyntaxError at runtime
when "/syntax_error_into_view"
@ -309,6 +316,22 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
assert_match(/ActionController::ParameterMissing/, body)
end
if defined?(DidYouMean) && DidYouMean.respond_to?(:correct_error)
test "rescue with suggestions" do
@app = DevelopmentApp
get "/not_found", headers: { "action_dispatch.show_exceptions" => true }
assert_response 404
assert_select("b", /Did you mean\?/)
assert_select("li", "hello")
get "/parameter_missing", headers: { "action_dispatch.show_exceptions" => true }
assert_response 400
assert_select("b", /Did you mean\?/)
assert_select("li", "valid_param_key")
end
end
test "rescue with HTML format for HTML API request" do
@app = ActionDispatch::DebugExceptions.new(Boomer.new(true), RoutesApp, :api)