From 5cf725acb6ae49682860b4af9c95052a44b51aed Mon Sep 17 00:00:00 2001 From: Petrik Date: Wed, 20 May 2020 09:55:30 +0200 Subject: [PATCH] Use html for DidYouMean suggestions on error pages The current suggestions are shown on a single line. Instead we can use some prettier formatting. --- .../rescues/_message_and_suggestions.html.erb | 18 +++++++++++++ .../templates/rescues/diagnostics.html.erb | 7 ++--- .../templates/rescues/unknown_action.html.erb | 2 +- .../test/dispatch/debug_exceptions_test.rb | 27 +++++++++++++++++-- 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 actionpack/lib/action_dispatch/middleware/templates/rescues/_message_and_suggestions.html.erb diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/_message_and_suggestions.html.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/_message_and_suggestions.html.erb new file mode 100644 index 0000000000..8d43478c6e --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/_message_and_suggestions.html.erb @@ -0,0 +1,18 @@ +<% if exception.respond_to?(:original_message) && exception.respond_to?(:corrections) %> +

<%= h exception.original_message %>

+ <% + # 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? %> + Did you mean? + + <% end %> +<% else %> +

<%= h exception.message %>

+<% end %> diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb index 57cdcf9aaf..8edf7fbc34 100644 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb @@ -8,11 +8,8 @@
-

- <%= h @exception.message %> - - <%= render "rescues/actions", exception: @exception, request: @request %> -

+ <%= 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 %> diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb index 259fb2bb3b..9448989806 100644 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb @@ -2,5 +2,5 @@

Unknown action

-

<%= h @exception.message %>

+ <%= render "rescues/message_and_suggestions", exception: @exception %>
diff --git a/actionpack/test/dispatch/debug_exceptions_test.rb b/actionpack/test/dispatch/debug_exceptions_test.rb index fbe84962cd..bdb17ffcaa 100644 --- a/actionpack/test/dispatch/debug_exceptions_test.rb +++ b/actionpack/test/dispatch/debug_exceptions_test.rb @@ -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)